// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
testq %rax, %rax
pushq $-0x1
popq %rbx
cmovnsq %r15, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r14
movq %rbx, %rdi
callq 0x2030
movq %rax, %rbx
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
testl %r13d, %r13d
jle 0x2439
movq %r14, %r15
movslq (%r12), %rdx
shlq $0x3, %rdx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x2110
xorpd %xmm2, %xmm2
cmpl $0x0, (%r12)
jle 0x2426
xorl %r14d, %r14d
movsd (%r15,%r14,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x23c9
jnp 0x241a
movq %r12, %rdi
movl %r14d, %esi
callq 0x306c
xorpd %xmm2, %xmm2
movslq 0x30(%rax), %rcx
testq %rcx, %rcx
jle 0x241a
movq 0x38(%rax), %rax
movsd (%r15,%r14,8), %xmm0
mulsd 0x2fe5(%rip), %xmm0     # 0x53d8
shlq $0x4, %rcx
xorl %edx, %edx
movslq (%rax,%rdx), %rsi
movsd 0x8(%rax,%rdx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rbx,%rsi,8), %xmm1
movsd %xmm1, (%rbx,%rsi,8)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x23f9
incq %r14
movslq (%r12), %rax
cmpq %rax, %r14
jl 0x23bb
incl %ebp
movq %rbx, %r14
movq %r15, %rbx
cmpl %r13d, %ebp
jne 0x2395
jmp 0x243c
movq %rbx, %r15
movslq (%r12), %rax
xorpd %xmm0, %xmm0
testq %rax, %rax
jle 0x246f
xorl %ecx, %ecx
movapd 0x2f9d(%rip), %xmm1     # 0x53f0
movsd (%r15,%rcx,8), %xmm2
subsd (%r14,%rcx,8), %xmm2
andpd %xmm1, %xmm2
addsd %xmm2, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x2453
movq 0x5b6a(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2fa0(%rip), %rsi      # 0x5420
movb $0x1, %al
callq 0x21e0
movslq (%r12), %rdx
shlq $0x3, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2110
movslq (%r12), %rcx
testq %rcx, %rcx
jle 0x24cb
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%rbx,%rax,8), %xmm1
cmpneqsd %xmm0, %xmm1
movq %xmm1, %rsi
subl %esi, %edx
incq %rax
cmpq %rax, %rcx
jne 0x24b0
jmp 0x24cd
xorl %edx, %edx
movq 0x5af4(%rip), %r12      # 0x7fc8
movq (%r12), %rdi
leaq 0x2f6c(%rip), %rsi      # 0x544b
xorl %eax, %eax
callq 0x21e0
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2220
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq 0x18(%rsp), %rax
cvtsi2sd %rax, %xmm0
subq 0x20(%rsp), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x2cb0(%rip), %xmm2     # 0x51c8
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq (%r12), %rdi
divsd %xmm2, %xmm0
leaq 0x2f49(%rip), %rsi      # 0x547c
movb $0x1, %al
callq 0x21e0
testq %r14, %r14
je 0x2547
movq %r14, %rdi
callq 0x21c0
testq %r15, %r15
je 0x2562
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21c0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq