// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movslq (%rdi), %rax
leaq (,%rax,8), %r12
testq %rax, %rax
pushq $-0x1
popq %rbx
cmovnsq %r12, %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 %r12, %rdx
callq 0x20c0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
movl (%r15), %eax
testl %r13d, %r13d
jle 0x2439
movq %r14, %r12
movslq %eax, %rdx
shlq $0x3, %rdx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x2110
xorpd %xmm2, %xmm2
movl (%r15), %eax
testl %eax, %eax
jle 0x2426
xorl %r14d, %r14d
movsd (%r12,%r14,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x23ca
jnp 0x241b
movq %r15, %rdi
movl %r14d, %esi
callq 0x3138
xorpd %xmm2, %xmm2
movslq 0x30(%rax), %rcx
testq %rcx, %rcx
jle 0x241b
movq 0x38(%rax), %rax
movsd (%r12,%r14,8), %xmm0
mulsd 0x303c(%rip), %xmm0     # 0x5430
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 0x23fa
incq %r14
movslq (%r15), %rax
cmpq %rax, %r14
jl 0x23bc
incl %ebp
movq %rbx, %r14
movq %r12, %rbx
cmpl %r13d, %ebp
jne 0x2397
jmp 0x243c
movq %rbx, %r12
xorpd %xmm0, %xmm0
testl %eax, %eax
jle 0x246c
movl %eax, %eax
xorl %ecx, %ecx
movapd 0x3000(%rip), %xmm1     # 0x5450
movsd (%r12,%rcx,8), %xmm2
subsd (%r14,%rcx,8), %xmm2
andpd %xmm1, %xmm2
addsd %xmm2, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x2450
movq 0x5b6d(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x3003(%rip), %rsi      # 0x5480
movb $0x1, %al
callq 0x21e0
movslq (%r15), %rdx
shlq $0x3, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2110
movslq (%r15), %rcx
testq %rcx, %rcx
jle 0x24c6
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 0x24ab
jmp 0x24c8
xorl %edx, %edx
movq 0x5af9(%rip), %r15      # 0x7fc8
movq (%r15), %rdi
leaq 0x2fd2(%rip), %rsi      # 0x54ab
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 0x2d0e(%rip), %xmm2     # 0x5220
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq (%r15), %rdi
divsd %xmm2, %xmm0
leaq 0x2fb0(%rip), %rsi      # 0x54dc
movb $0x1, %al
callq 0x21e0
testq %r14, %r14
je 0x2540
movq %r14, %rdi
callq 0x21c0
testq %r12, %r12
je 0x255b
movq %r12, %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