// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movslq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movslq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20d0
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22d0
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x48ec
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
callq 0x2150
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x48c6
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4823
jp 0x4823
jmp 0x48b8
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x3910
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x48b6
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd 0x3b6a(%rip), %xmm0     # 0x83e0
mulsd (%rax,%rcx,8), %xmm0
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd 0x8(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
movq -0x30(%rbp), %rax
movslq -0x70(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x4846
jmp 0x48b8
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x47f7
movq -0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x47cc
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x495b
movq -0x30(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x8c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movaps 0x3aba(%rip), %xmm1     # 0x83f0
pand %xmm1, %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x4901
movq 0x767e(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
movsd -0x88(%rbp), %xmm0
leaq 0x3aac(%rip), %rsi      # 0x8420
movb $0x1, %al
callq 0x2280
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
callq 0x2150
movl $0x0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x49f4
movq -0x18(%rbp), %rax
movslq -0x94(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x49d2
jp 0x49d2
jmp 0x49e1
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x49e3
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x49a7
movq 0x75cd(%rip), %rax      # 0xbfc8
movq (%rax), %rdi
movl -0x90(%rbp), %edx
movq -0x8(%rbp), %rax
movl (%rax), %ecx
leaq 0x3a3a(%rip), %rsi      # 0x844b
movb $0x0, %al
callq 0x2280
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22d0
movq -0x50(%rbp), %rax
subq -0x40(%rbp), %rax
cvtsi2sd %rax, %xmm0
movq -0x48(%rbp), %rax
subq -0x38(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x3789(%rip), %xmm2     # 0x81d0
divsd %xmm2, %xmm1
movsd 0x377d(%rip), %xmm2     # 0x81d0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movq 0x755e(%rip), %rax      # 0xbfc8
movq (%rax), %rdi
movsd -0xa0(%rbp), %xmm0
movsd 0x3753(%rip), %xmm1     # 0x81d0
divsd %xmm1, %xmm0
leaq 0x39f4(%rip), %rsi      # 0x847c
movb $0x1, %al
callq 0x2280
movq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x4aac
movq -0xa8(%rbp), %rdi
callq 0x2250
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x4ac9
movq -0xb0(%rbp), %rdi
callq 0x2250
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)