// File: /workspace/github_cpp_makefile_star2_O1/wenleix[P]EdgePPR/src/PPRCommon.cpp
// Function: ParamJacobiSolver(ParamGraph*, double*, double*, double const*, double)
void ParamJacobiSolver(ParamGraph *g, double *pref, double *pprValue,
					   const double *param, double eps) {
	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);

    //	Magic number 500
    for (int iIter = 0; iIter < 500; 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);
            double adjustDampFactor = DAMP_FACTOR;

        	ParamEdge *outEdge = vtx->outEdge;
        	for (int j = 0; j < vtx->outDeg; j++) {
        		ParamEdge *curEdge = &outEdge[j];
        		int etype = curEdge->GetIntEType();
        		int dstId = curEdge->dstId;
        		double contrib = preValue[i] * adjustDampFactor * param[etype] / vtx->outEdgeTypeCount[etype];
        		curValue[dstId] += contrib;
        	}
        }

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

		if (iIter % 10 == 0) {
			double L1Change = 0.0;
			for (int i = 0; i < g->numVertex; i++)
				L1Change += fabs(curValue[i] - preValue[i]);
			if (L1Change < eps) {
				fprintf(stderr, "DEBUG: L1 small enough at iteration %d, early quit :)\n", iIter);
				break;
			}
		}

    }

    //	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(stderr, "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 $0x48, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rcx, %r13
movq %rdx, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movslq (%rdi), %rax
leaq (,%rax,8), %r14
testq %rax, %rax
pushq $-0x1
popq %rbx
cmovnsq %r14, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %rbp
movq %rbx, %rdi
callq 0x2030
movq %rax, %rbx
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20c0
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
xorl %r15d, %r15d
movq %rbx, %r14
movq %rbp, %rbx
movslq (%r12), %rdx
shlq $0x3, %rdx
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x2110
xorpd %xmm3, %xmm3
movl (%r12), %ecx
testl %ecx, %ecx
jle 0x267f
xorl %ebp, %ebp
movsd (%rbx,%rbp,8), %xmm0
ucomisd %xmm3, %xmm0
jne 0x2612
jnp 0x2673
movq %r12, %rdi
movl %ebp, %esi
callq 0x306c
xorpd %xmm3, %xmm3
movslq 0x30(%rax), %rcx
testq %rcx, %rcx
jle 0x2673
movq 0x38(%rax), %rdx
movsd (%rbx,%rbp,8), %xmm0
mulsd 0x2d9e(%rip), %xmm0     # 0x53d8
shlq $0x4, %rcx
xorl %esi, %esi
movslq 0x4(%rdx,%rsi), %rdi
movslq (%rdx,%rsi), %r8
movsd (%r13,%rdi,8), %xmm1
cvtsi2sdl 0x40(%rax,%rdi,4), %xmm2
mulsd %xmm0, %xmm1
divsd %xmm2, %xmm1
addsd (%r14,%r8,8), %xmm1
movsd %xmm1, (%r14,%r8,8)
addq $0x10, %rsi
cmpq %rsi, %rcx
jne 0x2640
incq %rbp
movslq (%r12), %rcx
cmpq %rcx, %rbp
jl 0x2605
xorl %esi, %esi
movl %r15d, %eax
xorl %edx, %edx
pushq $0xa
popq %rdi
divl %edi
testl %edx, %edx
jne 0x26da
testl %ecx, %ecx
movapd 0x2d57(%rip), %xmm1     # 0x53f0
jle 0x26c0
movl %ecx, %eax
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
movsd (%rbx,%rcx,8), %xmm0
subsd (%r14,%rcx,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rcx
cmpq %rcx, %rax
jne 0x26a3
jmp 0x26c4
xorpd %xmm2, %xmm2
xorl %esi, %esi
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
ja 0x26f3
ucomisd %xmm2, %xmm0
ja 0x26da
xorl %esi, %esi
testl %esi, %esi
jne 0x2725
incl %r15d
movq %r14, %rbp
cmpl $0x1f4, %r15d           # imm = 0x1F4
jne 0x25dc
jmp 0x2725
movq 0x58e6(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2d91(%rip), %rsi      # 0x5495
movl %r15d, %edx
xorl %eax, %eax
movsd %xmm2, 0x18(%rsp)
callq 0x21e0
movsd 0x18(%rsp), %xmm2
movsd 0x8(%rsp), %xmm0
pushq $0x2
popq %rsi
jmp 0x26d2
movslq (%r12), %rax
xorpd %xmm0, %xmm0
testq %rax, %rax
movapd 0x2cb8(%rip), %xmm2     # 0x53f0
jle 0x2757
xorl %ecx, %ecx
movsd (%rbx,%rcx,8), %xmm1
subsd (%r14,%rcx,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x273c
movq 0x5882(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2cb8(%rip), %rsi      # 0x5420
movb $0x1, %al
callq 0x21e0
movslq (%r12), %rdx
shlq $0x3, %rdx
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2110
movslq (%r12), %rcx
xorl %edx, %edx
testq %rcx, %rcx
jle 0x27b4
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%r15,%rax,8), %xmm1
cmpneqsd %xmm0, %xmm1
movq %xmm1, %rsi
subl %esi, %edx
incq %rax
cmpq %rax, %rcx
jne 0x279a
movq 0x5825(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2c86(%rip), %rsi      # 0x544b
xorl %eax, %eax
callq 0x21e0
leaq 0x38(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2220
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq 0x28(%rsp), %rax
cvtsi2sd %rax, %xmm0
subq 0x30(%rsp), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x29ca(%rip), %xmm2     # 0x51c8
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x57b7(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
divsd %xmm2, %xmm0
leaq 0x2c5d(%rip), %rsi      # 0x547c
movb $0x1, %al
callq 0x21e0
testq %r14, %r14
je 0x2833
movq %r14, %rdi
callq 0x21c0
testq %rbx, %rbx
je 0x284e
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21c0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)