// File: /workspace/github_cpp_makefile_star2_O2/wenleix[P]EdgePPR/src/ParamPPR.cpp
// Function: main
int main(int argc, char** argv) {
	if (argc != 7) {
		fprintf(stderr, "Usage: %s [ParamGraph.txt] [NumUseVtx] [ParameterFile] "
				"[OutputPrefix] [OutFormat: bin/txt] [NumThread]\n",
			    argv[0]);
		return 1;
	}

	FILE *gfile = fopen(argv[1], "r");
	int numUseVtx = atoi(argv[2]);	//	# Useful Vertices
	FILE *paramFile = fopen(argv[3], "r");
	char* outputPrefix = argv[4];
	char *outFormat = argv[5];
	int numThread = atoi(argv[6]);

	assert(strcmp(outFormat, "bin") == 0 || strcmp(outFormat, "txt") == 0);
	ParamGraph *g = ParamGraph::ReadTextFile(gfile);

	int paramId = 0;
	double *pref = new double[g->numVertex];
	memset(pref, 0, sizeof(double) * g->numVertex);
	for (int i = 0; i < numUseVtx; i++)
		pref[i] = 1.0 - DAMP_FACTOR;

	vector<double> params;
	//	Read all the parameters
	while (true) {
		double p;
		if (fscanf(paramFile, "%lf", &p) == EOF)
			break;
		params.push_back(p);
	}

	assert(params.size() % ParamGraph_NumEdgeTypes == 0);
	int numParam = params.size() / ParamGraph_NumEdgeTypes;
	fprintf(stdout, "INFO: # Params = %lu. \n", params.size() / ParamGraph_NumEdgeTypes);
	int *paramIdx = new int[numParam];		//	Random shuffle the parameter indices assigned to each thread, to get better workload balance.
	for (int i = 0; i < numParam; i++)
		paramIdx[i] = i;
	random_shuffle(paramIdx, paramIdx + numParam);

	ThreadConfig *conf = new ThreadConfig[numThread];
	pthread_t *threads = new pthread_t[numThread];
	for (int i = 0; i < numThread; i++) {
		conf[i].threadId = i;
		conf[i].params = &params[0];
		conf[i].start = i * numParam / numThread;
		conf[i].end = (i + 1) * numParam / numThread;
		conf[i].numUseVtx = numUseVtx;
		conf[i].outputPrefix = outputPrefix;
		conf[i].pref = pref;
		conf[i].g = g;
		conf[i].paramIdx = paramIdx;
		conf[i].outputFormat = outFormat;

		//	Start thread
		int rc = pthread_create(&threads[i], NULL /* attr */, WorkerThread, (void*)&conf[i]);
		assert(rc == 0);
	}

	for (int i = 0; i < numThread; i++)
		pthread_join(threads[i], NULL /* retval */);



	delete g;
	delete [] conf;
	delete [] threads;
	delete [] paramIdx;
	fclose(gfile);
	fclose(paramFile);

	//	Finish
	pthread_exit(NULL);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
cmpl $0x7, %edi
je 0x2561
movq 0x5aa5(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x2b37(%rip), %rsi      # 0x507f
xorl %eax, %eax
callq 0x21f0
pushq $0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x2b7b(%rip), %r14      # 0x50e7
movq %r14, %rsi
callq 0x21a0
movq %rax, 0x28(%rsp)
movq 0x10(%rbx), %rdi
callq 0x2210
movl %eax, 0x4(%rsp)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0x21a0
movq %rax, %r14
movq 0x20(%rbx), %rax
movq %rax, 0x48(%rsp)
movq 0x28(%rbx), %r15
movq 0x30(%rbx), %rdi
callq 0x2210
movl %eax, (%rsp)
leaq 0x2b34(%rip), %rsi      # 0x50e9
movq %r15, 0x38(%rsp)
movq %r15, %rdi
callq 0x21e0
testl %eax, %eax
je 0x25df
leaq 0x2b20(%rip), %rsi      # 0x50ed
movq 0x38(%rsp), %rdi
callq 0x21e0
testl %eax, %eax
jne 0x2712
movq 0x28(%rsp), %rdi
callq 0x2b34
movq %rax, 0x30(%rsp)
movslq (%rax), %rax
leaq (,%rax,8), %r15
testq %rax, %rax
pushq $-0x1
popq %rbp
movq %r15, %rdi
cmovsq %rbp, %rdi
callq 0x2030
movq %rax, %rbx
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20d0
movl 0x4(%rsp), %eax
testl %eax, %eax
cmovlel %r12d, %eax
movabsq $0x3fc3333333333334, %rcx # imm = 0x3FC3333333333334
cmpq %r12, %rax
je 0x2640
movq %rcx, (%rbx,%r12,8)
incq %r12
jmp 0x2632
leaq 0x50(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
leaq 0x2b00(%rip), %r12      # 0x5158
leaq 0x70(%rsp), %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2290
cmpl $-0x1, %eax
je 0x267f
movq %r15, %rdi
movq %r13, %rsi
callq 0x28d4
jmp 0x265d
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
pushq $0x7
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x10(%rsp)
testq %rdx, %rdx
jne 0x272b
movq 0x591e(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
leaq 0x2ad5(%rip), %rsi      # 0x5189
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x21f0
movslq %r12d, %r12
movq %r12, %rax
shrq $0x3e, %rax
leaq (,%r12,4), %rcx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq 0x8(%rsp), %rdi
cmpq %r15, %rax
je 0x2748
movl %r15d, (%rdi,%r15,4)
incq %r15
jmp 0x2704
leaq 0x29d8(%rip), %rdi      # 0x50f1
leaq 0x2a10(%rip), %rsi      # 0x5130
leaq 0x2a1a(%rip), %rcx      # 0x5141
pushq $0x48
jmp 0x2742
leaq 0x2a2a(%rip), %rdi      # 0x515c
leaq 0x29f7(%rip), %rsi      # 0x5130
leaq 0x2a01(%rip), %rcx      # 0x5141
pushq $0x5a
popq %rdx
callq 0x20e0
leaq (%rdi,%r12,4), %rsi
callq 0x28f6
movslq (%rsp), %r15
pushq $0x48
popq %rcx
movq %r15, %rax
mulq %rcx
cmovoq %rbp, %rax
movq %rax, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
shlq $0x3, %r15
cmpl $0x0, (%rsp)
cmovsq %rbp, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
movl (%rsp), %eax
testl %eax, %eax
cmovlel %r12d, %eax
movq %rax, 0x40(%rsp)
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r13
xorl %ebp, %ebp
cmpq %rbp, 0x40(%rsp)
je 0x284b
movl %ebp, (%r15)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%r15)
movl %r12d, %eax
cltd
movl (%rsp), %ecx
idivl %ecx
movl %eax, 0x10(%r15)
incq %rbp
addl 0x10(%rsp), %r12d
movl %r12d, %eax
cltd
idivl %ecx
movl %eax, 0x14(%r15)
movl 0x4(%rsp), %eax
movl %eax, 0x18(%r15)
movq 0x48(%rsp), %rax
movq %rax, 0x20(%r15)
movq %rbx, 0x28(%r15)
movq 0x30(%rsp), %rax
movq %rax, 0x40(%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x30(%r15)
movq 0x38(%rsp), %rax
movq %rax, 0x38(%r15)
movq %r13, %rdi
xorl %esi, %esi
leaq -0x45b(%rip), %rdx      # 0x23bc
movq %r15, %rcx
callq 0x2280
addq $0x8, %r13
addq $0x48, %r15
testl %eax, %eax
je 0x27a5
leaq 0x296b(%rip), %rdi      # 0x51a1
leaq 0x28f3(%rip), %rsi      # 0x5130
leaq 0x28fd(%rip), %rcx      # 0x5141
pushq $0x72
jmp 0x2742
xorl %ebx, %ebx
movq 0x18(%rsp), %r15
cmpq %rbx, 0x40(%rsp)
je 0x2869
movq (%r15,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2130
incq %rbx
jmp 0x2852
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x3000
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x2160
movq 0x20(%rsp), %rdi
callq 0x21d0
movq %r15, %rdi
callq 0x21d0
movq 0x8(%rsp), %rdi
callq 0x21d0
movq 0x28(%rsp), %rdi
callq 0x2140
movq %r14, %rdi
callq 0x2140
xorl %edi, %edi
callq 0x2260
jmp 0x28be
jmp 0x28be
jmp 0x28be
jmp 0x28be
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x295e
movq %rbx, %rdi
callq 0x2250
nop