// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rcx, %r13
movq %rdx, (%rsp)
movq %rsi, 0x10(%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
xorl %r15d, %r15d
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20c0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
movl (%r12), %ecx
movq %rbx, %r14
movq %rbp, %rbx
movslq %ecx, %rdx
shlq $0x3, %rdx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x2110
xorpd %xmm3, %xmm3
movl (%r12), %ecx
testl %ecx, %ecx
jle 0x267a
xorl %ebp, %ebp
movsd (%rbx,%rbp,8), %xmm0
ucomisd %xmm3, %xmm0
jne 0x260d
jnp 0x266e
movq %r12, %rdi
movl %ebp, %esi
callq 0x3138
xorpd %xmm3, %xmm3
movslq 0x30(%rax), %rcx
testq %rcx, %rcx
jle 0x266e
movq 0x38(%rax), %rdx
movsd (%rbx,%rbp,8), %xmm0
mulsd 0x2dfb(%rip), %xmm0     # 0x5430
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 0x263b
incq %rbp
movslq (%r12), %rcx
cmpq %rcx, %rbp
jl 0x2600
movl %r15d, %eax
xorl %edx, %edx
pushq $0xa
popq %rsi
divl %esi
testl %edx, %edx
movapd 0x2dc2(%rip), %xmm2     # 0x5450
jne 0x26c9
testl %ecx, %ecx
jle 0x26b9
movl %ecx, %eax
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%rbx,%rdx,8), %xmm1
subsd (%r14,%rdx,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x269c
jmp 0x26bd
xorpd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x26de
incl %r15d
movq %r14, %rbp
cmpl $0x1f4, %r15d           # imm = 0x1F4
jne 0x25d8
jmp 0x2704
movq 0x58fb(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2e06(%rip), %rsi      # 0x54f5
movl %r15d, %edx
xorl %eax, %eax
callq 0x21e0
movaps 0x2d50(%rip), %xmm2     # 0x5450
movl (%r12), %ecx
xorpd %xmm0, %xmm0
testl %ecx, %ecx
jle 0x272b
movl %ecx, %eax
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 0x2710
movq 0x58ae(%rip), %r15      # 0x7fe0
movq (%r15), %rdi
leaq 0x2d44(%rip), %rsi      # 0x5480
movb $0x1, %al
callq 0x21e0
movslq (%r12), %rdx
shlq $0x3, %rdx
movq (%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2110
movslq (%r12), %rcx
xorl %edx, %edx
testq %rcx, %rcx
jle 0x2788
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%r13,%rax,8), %xmm1
cmpneqsd %xmm0, %xmm1
movq %xmm1, %rsi
subl %esi, %edx
incq %rax
cmpq %rax, %rcx
jne 0x276d
movq (%r15), %rdi
leaq 0x2d19(%rip), %rsi      # 0x54ab
xorl %eax, %eax
callq 0x21e0
leaq 0x28(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2220
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq 0x18(%rsp), %rax
cvtsi2sd %rax, %xmm0
subq 0x20(%rsp), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x2a55(%rip), %xmm2     # 0x5220
divsd %xmm2, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x57ea(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
divsd %xmm2, %xmm0
leaq 0x2cf0(%rip), %rsi      # 0x54dc
movb $0x1, %al
callq 0x21e0
testq %r14, %r14
je 0x2800
movq %r14, %rdi
callq 0x21c0
testq %rbx, %rbx
je 0x281b
movq %rbx, %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
nopw (%rax,%rax)