// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -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 -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, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20d0
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22d0
movl $0x0, -0x5c(%rbp)
cmpl $0x1f4, -0x5c(%rbp)     # imm = 0x1F4
jge 0x4dae
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
callq 0x2150
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x4ccb
movq -0x30(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4bda
jp 0x4bda
jmp 0x4cbd
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0x3910
movq %rax, -0x68(%rbp)
movsd 0x37ee(%rip), %xmm0     # 0x83e0
movsd %xmm0, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x4cbb
movq -0x78(%rbp), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x4460
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
mulsd -0x70(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0x8c(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq -0x68(%rbp), %rax
movslq -0x8c(%rbp), %rcx
cvtsi2sdl 0x40(%rax,%rcx,4), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movq -0x38(%rbp), %rax
movslq -0x90(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x4c0a
jmp 0x4cbd
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x4bae
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x5c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x4d9e
xorps %xmm0, %xmm0
movsd %xmm0, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x4d6c
movq -0x38(%rbp), %rax
movslq -0xac(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0xac(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movaps 0x36a9(%rip), %xmm1     # 0x83f0
pand %xmm1, %xmm0
addsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x4d12
movsd -0xa8(%rbp), %xmm1
movsd -0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x4d9c
movq 0x725a(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
movl -0x5c(%rbp), %edx
leaq 0x3702(%rip), %rsi      # 0x8495
movb $0x0, %al
callq 0x2280
jmp 0x4dae
jmp 0x4d9e
jmp 0x4da0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4b82
xorps %xmm0, %xmm0
movsd %xmm0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x4e1d
movq -0x38(%rbp), %rax
movslq -0xbc(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0xbc(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movaps 0x35f8(%rip), %xmm1     # 0x83f0
pand %xmm1, %xmm0
addsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x4dc3
movq 0x71bc(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
movsd -0xb8(%rbp), %xmm0
leaq 0x35ea(%rip), %rsi      # 0x8420
movb $0x1, %al
callq 0x2280
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
callq 0x2150
movl $0x0, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x4eb6
movq -0x18(%rbp), %rax
movslq -0xc4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4e94
jp 0x4e94
jmp 0x4ea3
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x4ea5
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x4e69
movq 0x7123(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
movl -0xc0(%rbp), %edx
movq -0x8(%rbp), %rax
movl (%rax), %ecx
leaq 0x3578(%rip), %rsi      # 0x844b
movb $0x0, %al
callq 0x2280
leaq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22d0
movq -0x58(%rbp), %rax
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
movq -0x50(%rbp), %rax
subq -0x40(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x32c7(%rip), %xmm2     # 0x81d0
divsd %xmm2, %xmm1
movsd 0x32bb(%rip), %xmm2     # 0x81d0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq 0x709c(%rip), %rax      # 0xbfc8
movq (%rax), %rdi
movsd -0xd0(%rbp), %xmm0
movsd 0x3291(%rip), %xmm1     # 0x81d0
divsd %xmm1, %xmm0
leaq 0x3532(%rip), %rsi      # 0x847c
movb $0x1, %al
callq 0x2280
movq -0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x4f6e
movq -0xd8(%rbp), %rdi
callq 0x2250
movq -0x38(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x4f8b
movq -0xe0(%rbp), %rdi
callq 0x2250
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)