// File: /workspace/github_cpp_makefile_star2_O2/wenleix[P]EdgePPR/src/PPRCommon.cpp
// Function: JacobiSolver(ParamGraph*, double*, double*, int)
void JacobiSolver(ParamGraph *g, double *pref, double *pprValue, int numIter) {
	double *preValue = new double[g->numVertex];
	double *curValue = new double[g->numVertex];

	memset(preValue, 0, sizeof(double) * g->numVertex);

    timeval start, end;
    gettimeofday(&start, NULL);

    for (int iIter = 0; iIter < numIter; iIter++) {
    	memcpy(curValue, pref, sizeof(double) * g->numVertex);

        for (int i = 0; i < g->numVertex; i++) {
            if (preValue[i] == 0)
                continue;

            ParamVertex *vtx = g->GetVertex(i);
        	ParamEdge *outEdge = vtx->outEdge;
        	for (int j = 0; j < vtx->outDeg; j++) {
        		int dstId = outEdge[j].dstId;
        		double contrib = preValue[i] * DAMP_FACTOR * outEdge[j].weight;
        		curValue[dstId] += contrib;
        	}
        }

		double* temp = curValue;
		curValue = preValue;
		preValue = temp;
    }

    //	Calculate the L1 change in the last iteration.
    double L1Change = 0.0;
    for (int i = 0; i < g->numVertex; i++)
    	L1Change += fabs(curValue[i] - preValue[i]);

    fprintf(stderr, "DEBUG: L1Change in Last Iteration = %.4le\n", L1Change);

    //  Copy the result out
	memcpy(pprValue, preValue, sizeof(double) * g->numVertex);

    int nonZeroCount = 0;
    for (int i = 0; i < g->numVertex; i++)
        if (pprValue[i] != 0.0) nonZeroCount++;
    fprintf(stdout, "DEBUG: # non zero entries / All vertex: %d / %d\n", nonZeroCount, g->numVertex);

    gettimeofday(&end, NULL);

    double elapsed_ms = (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0;
    fprintf(stdout, "Running Time: %.2lf sec\n", elapsed_ms / 1000.0);

    delete [] preValue;
    delete [] curValue;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r13d
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movslq (%rdi), %rax
leaq (,%rax,8), %r14
testq %rax, %rax
pushq $-0x1
popq %r15
cmovnsq %r14, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %rbx
movq %r15, %rdi
callq 0x2030
movq %rax, %r15
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20d0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2230
testl %r13d, %r13d
cmovlel %ebp, %r13d
movl %r13d, 0x4(%rsp)
xorl %r13d, %r13d
movq %r15, %r14
movslq (%r12), %rdx
cmpl 0x4(%rsp), %r13d
je 0x3258
shlq $0x3, %rdx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x2120
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
movslq (%r12), %rax
cmpq %rax, %r15
jge 0x324a
movsd (%rbx,%r15,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x31f6
jp 0x31f6
incq %r15
jmp 0x31da
movq %r12, %rdi
movl %r15d, %esi
callq 0x2ad8
movsd 0x21cf(%rip), %xmm2     # 0x53d8
xorpd %xmm1, %xmm1
movq 0x38(%rax), %rcx
movl 0x30(%rax), %eax
testl %eax, %eax
cmovlel %ebp, %eax
shlq $0x4, %rax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x31f1
movslq (%rcx,%rdx), %rsi
movsd (%rbx,%r15,8), %xmm0
mulsd %xmm2, %xmm0
mulsd 0x8(%rcx,%rdx), %xmm0
addsd (%r14,%rsi,8), %xmm0
movsd %xmm0, (%r14,%rsi,8)
addq $0x10, %rdx
jmp 0x321f
incl %r13d
movq %rbx, %r15
movq %r14, %rbx
jmp 0x31b0
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
xorpd %xmm0, %xmm0
movapd 0x2185(%rip), %xmm1     # 0x53f0
movq 0x8(%rsp), %r15
cmpq %rax, %rdx
je 0x328d
movsd (%r14,%rax,8), %xmm2
subsd (%rbx,%rax,8), %xmm2
andpd %xmm1, %xmm2
addsd %xmm2, %xmm0
incq %rax
jmp 0x3270
movq 0x4d4c(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2182(%rip), %rsi      # 0x5420
movb $0x1, %al
callq 0x21f0
movslq (%r12), %rdx
shlq $0x3, %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2120
movl (%r12), %ecx
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq %rax, %rsi
je 0x32ea
movsd (%r15,%rax,8), %xmm1
cmpneqsd %xmm0, %xmm1
movq %xmm1, %rdi
subl %edi, %edx
incq %rax
jmp 0x32ce
movq 0x4cd7(%rip), %r12      # 0x7fc8
movq (%r12), %rdi
leaq 0x214f(%rip), %rsi      # 0x544b
xorl %eax, %eax
callq 0x21f0
leaq 0x28(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2230
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq 0x18(%rsp), %rax
cvtsi2sd %rax, %xmm0
subq 0x20(%rsp), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x1e93(%rip), %xmm2     # 0x51c8
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq (%r12), %rdi
divsd %xmm2, %xmm0
leaq 0x212c(%rip), %rsi      # 0x547c
movb $0x1, %al
callq 0x21f0
testq %rbx, %rbx
je 0x3364
movq %rbx, %rdi
callq 0x21d0
testq %r14, %r14
je 0x337f
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21d0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq