// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movq %rcx, %r13
movq %rdx, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r15
movslq (%rdi), %rax
leaq (,%rax,8), %rbx
testq %rax, %rax
pushq $-0x1
popq %r14
cmovnsq %rbx, %r14
movq %r14, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %rbp
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20d0
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x2230
movapd 0x1fee(%rip), %xmm2     # 0x53f0
xorl %r8d, %r8d
movq %rbp, %rbx
cmpl $0x1f4, %r8d            # imm = 0x1F4
je 0x3511
movl %r8d, 0xc(%rsp)
movslq (%r15), %rdx
shlq $0x3, %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x2120
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movslq (%r15), %rcx
cmpq %rcx, %rbp
jge 0x34b3
movsd (%r12,%rbp,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x344f
jp 0x344f
incq %rbp
jmp 0x3434
movq %r15, %rdi
movl %ebp, %esi
callq 0x2ad8
movsd 0x1f77(%rip), %xmm3     # 0x53d8
xorpd %xmm2, %xmm2
movq 0x38(%rax), %rcx
movl 0x30(%rax), %edx
testl %edx, %edx
cmovlel %r14d, %edx
shlq $0x4, %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x344a
movslq 0x4(%rcx,%rsi), %rdi
movsd (%r12,%rbp,8), %xmm0
mulsd %xmm3, %xmm0
mulsd (%r13,%rdi,8), %xmm0
movslq (%rcx,%rsi), %r8
cvtsi2sdl 0x40(%rax,%rdi,4), %xmm1
divsd %xmm1, %xmm0
addsd (%rbx,%r8,8), %xmm0
movsd %xmm0, (%rbx,%r8,8)
addq $0x10, %rsi
jmp 0x3478
movl 0xc(%rsp), %r8d
movl %r8d, %eax
xorl %edx, %edx
pushq $0xa
popq %rsi
divl %esi
testl %edx, %edx
movapd 0x1f24(%rip), %xmm2     # 0x53f0
jne 0x3503
testl %ecx, %ecx
cmovlel %r14d, %ecx
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq %rax, %rcx
je 0x34f7
movsd (%r12,%rax,8), %xmm1
subsd (%rbx,%rax,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x34da
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3519
incl %r8d
movq %r12, %rbp
movq %rbx, %r12
jmp 0x3405
movq %rbx, %r13
movq %r12, %rbx
jmp 0x353e
movq 0x4ac0(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x1f6b(%rip), %rsi      # 0x5495
movl %r8d, %edx
xorl %eax, %eax
callq 0x21f0
movaps 0x1eb5(%rip), %xmm2     # 0x53f0
movq %r12, %r13
movl (%r15), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %r12
cmpq %rcx, %rax
je 0x356f
movsd (%r13,%rcx,8), %xmm1
subsd (%rbx,%rcx,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rcx
jmp 0x3551
movq 0x4a6a(%rip), %r14      # 0x7fe0
movq (%r14), %rdi
leaq 0x1ea0(%rip), %rsi      # 0x5420
movb $0x1, %al
callq 0x21f0
movslq (%r15), %rdx
shlq $0x3, %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2120
movl (%r15), %ecx
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq %rax, %rsi
je 0x35ca
movsd (%r12,%rax,8), %xmm1
cmpneqsd %xmm0, %xmm1
movq %xmm1, %rdi
subl %edi, %edx
incq %rax
jmp 0x35ae
movq (%r14), %rdi
leaq 0x1e77(%rip), %rsi      # 0x544b
xorl %eax, %eax
callq 0x21f0
leaq 0x38(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2230
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x28(%rsp), %rax
cvtsi2sd %rax, %xmm0
subq 0x30(%rsp), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x1bbb(%rip), %xmm2     # 0x51c8
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x49a8(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
divsd %xmm2, %xmm0
leaq 0x1e4e(%rip), %rsi      # 0x547c
movb $0x1, %al
callq 0x21f0
testq %rbx, %rbx
je 0x3642
movq %rbx, %rdi
callq 0x21d0
testq %r13, %r13
je 0x365d
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21d0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq