// File: /workspace/github_cpp_makefile_star2_O2/Mengjintao[P]SWAPCounter/src/kmerGraph.cpp
// Function: kmerGraph::constructKmerGraph(parameter*, MPIEnviroment*, bloomFilter*)
int kmerGraph::constructKmerGraph(parameter *parameters, MPIEnviroment *MPIcontrol, bloomFilter *bFilter)
{
	clock_t IOTime=0, IOTimeRead=0, commTime=0, compTime=0, totTime=0, checkTime=0, t0, t1, t2;
	char message[1024];
	long long int readBases = min((long long int) MPIcontrol->nprocs/MPIcontrol->groupProc*1024*16, (long long int) BUF_SIZE);
	char          *readBuf = NULL; //new char [readBases*2+2];
	if(MPIcontrol->rank%MPIcontrol->groupProc==0)	readBuf = new char [readBases*2+2];	
	long long int readEnd=0, readStart=0, readLen=0, readRound=0;
	
	t0 = clock();
	MPIcontrol->File_open(parameters->fastaPath);
	
//	printf("Proc %d: %llu End\n", MPIcontrol->rank, readRound);
//	fflush(stdout);
	MPIcontrol->File_locate();
	t1 = clock();
	IOTime += t1-t0;
	totTime += t1-t0;
	int endTag = 1, totalTag = 0;
	
	do
	{
		t0 = clock();
		readLen = MPIcontrol->File_read(readBuf+readEnd, readBases);
		if(readBuf)	readBuf[readEnd+readLen] = 0;
		readEnd += readLen;		

		//losed the last sequence
//		if(MPIcontrol->datasize==0&&MPIcontrol->rank==MPIcontrol->nprocs-1)	{readBuf[readEnd]='>'; readBuf[++readEnd]=0;}	
		t1 = clock();
		IOTimeRead += t1-t0;
		if(DEBUG)
		{
			printf("Proc %d: %llu File_readLen=%llu, read_offset=%llu(%llu)\n", MPIcontrol->rank, readRound, readLen, MPIcontrol->read_offset-MPIcontrol->start_pos, MPIcontrol->datasize);
			fflush(stdout);
		}
		long long int localEnd = readEnd;
		
		if(readBuf)
		{
			while(localEnd>0 && readBuf[localEnd]!='>')	localEnd--;
			assert(readBuf[localEnd]=='>'||localEnd==0);
			readStart=localEnd;
		}
		//redistributed to groupProcs 	
		dataShuttleBetweenGroupProcs(parameters, MPIcontrol, readBuf, localEnd, readRound);

		extractKmerGraph(parameters, MPIcontrol, bFilter, readBuf, localEnd, readRound, commTime, checkTime);

		endTag=1, totalTag=0;
		if(MPIcontrol->datasize)	endTag=0;
		MPI_Allreduce(&endTag, &totalTag, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);

		if(readBuf)	
		{
			for(long long int i=0; i<readEnd-readStart; i++)	readBuf[i] = readBuf[readStart+i];
			readBuf[readEnd-readStart] = 0;
			readEnd   = readEnd-readStart;			
			readStart = 0;
		}

		t2 = clock();
		compTime += t2-t1;
		totTime  += t2-t0;
		readRound++;
		
		if(MPIcontrol->rank==0)
		{
			sprintf(message, "Proc %d: round %llu\n", MPIcontrol->rank, readRound);
			MPIcontrol->print(message);
		}
	}while(totalTag<MPIcontrol->nprocs);

	if(readBuf)
	{	
		for(long long int i=0; i<readEnd-readStart; i++)	readBuf[i] = readBuf[readStart+i];
		readBuf[readEnd-readStart] = 0;
	}
	readEnd = readEnd-readStart;			
	readStart = 0;
	long long int localEnd = readEnd;
	extractKmerGraph(parameters, MPIcontrol, bFilter, readBuf, localEnd, readRound, commTime, checkTime);
/*
	long long int tot_kmol=0, cur_kmol=0;
	
	for(unordered_map<unsigned long long, unsigned long long>::iterator I=kmolecules.begin(); I!=kmolecules.end(); I++)
//	for(auto I: kmolecules)
	{
		unsigned char *p = (unsigned char *) &(I->second);
		int t=0;
		for(int j=0;j<8;j++)	t += p[j];
		if(t>parameters->cutoffThreshold)	cur_kmol++;	
	}

	MPI_Allreduce(&cur_kmol, &tot_kmol, 1, MPI_LONG_LONG_INT, MPI_SUM, MPI_COMM_WORLD);
	printf("Proc %d: %llu Num of filtered kmolecules is %llu(totNum is %llu)\n", MPIcontrol->rank, readRound, cur_kmol, tot_kmol);
*/
	clock_t avgIOTime, avgIOTimeRead, avgCompTime, avgCommTime, avgTotTime, avgCheckTime;
	MPI_Allreduce(&IOTime,   &avgIOTime,   1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	MPI_Allreduce(&IOTimeRead, &avgIOTimeRead, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	MPI_Allreduce(&compTime, &avgCompTime, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	MPI_Allreduce(&commTime, &avgCommTime, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	MPI_Allreduce(&totTime,  &avgTotTime,  1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	MPI_Allreduce(&checkTime,  &avgCheckTime,  1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
	avgIOTime   = avgIOTime/MPIcontrol->nprocs;
	avgIOTimeRead = avgIOTimeRead/MPIcontrol->nprocs;
	avgCommTime = avgCommTime/MPIcontrol->nprocs;
	avgCompTime = avgCompTime/MPIcontrol->nprocs;
	avgTotTime  = avgTotTime/MPIcontrol->nprocs;
	avgCheckTime = avgCheckTime/MPIcontrol->nprocs;
	
	long long int tot_kmol=0, cur_kmol=kmolecules.size();
	MPI_Allreduce(&cur_kmol, &tot_kmol, 1, MPI_LONG_LONG_INT, MPI_SUM, MPI_COMM_WORLD);

	if(DEBUG)
	{	
		sprintf(message, "Proc %d: %llu Num of kmolecules is %llu(totNum is %llu)", MPIcontrol->rank, readRound, cur_kmol, tot_kmol);
		MPIcontrol->print(message);
	}

	if(MPIcontrol->rank==0)	
	{
		printf("IOTime=%.5fs IOTimeRead=%.5fs compTime=%.5fs commTime=%.5fs totTime=%.5fs checkTime=%.5fs\n", avgIOTime/(float)CLOCKS_PER_SEC, avgIOTimeRead/(float)CLOCKS_PER_SEC, avgCompTime/(float)CLOCKS_PER_SEC, avgCommTime/(float)CLOCKS_PER_SEC, avgTotTime/(float)CLOCKS_PER_SEC, avgCheckTime/(float)CLOCKS_PER_SEC);
	}

	MPIcontrol->File_close();
	if(readBuf)	delete [] readBuf;
	readBuf = NULL;
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
andq $0x0, 0xa0(%rsp)
andq $0x0, 0x58(%rsp)
andq $0x0, 0x98(%rsp)
movq %rdx, %r10
movq %rdx, 0x28(%rsp)
andq $0x0, 0x50(%rsp)
movslq 0x4(%rdx), %rax
movslq 0x278(%rdx), %r9
cqto
idivq %r9
movq %rax, %r8
movq %rax, %r11
shlq $0xe, %r11
movl $0x2000000, %eax        # imm = 0x2000000
cmpq %rax, %r11
cmovgeq %rax, %r11
movq %rcx, 0x60(%rsp)
andq $0x0, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movl (%r10), %eax
cltd
idivl %r9d
testl %edx, %edx
movq %r11, 0xa8(%rsp)
je 0x3454
xorl %r14d, %r14d
jmp 0x3473
leaq 0x2(,%r11,2), %rax
testq %r8, %r8
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movq %rax, 0x48(%rsp)
callq 0x2170
movq %rax, %r15
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0x52cc
movq %rbx, %rdi
callq 0x5366
callq 0x2170
subq %r15, %rax
movq %rbx, %r15
movq %rax, 0xb8(%rsp)
movq %rax, 0x90(%rsp)
leaq 0x24(%rsp), %rax
movl $0x1, (%rax)
leaq 0x20(%rsp), %rax
andl $0x0, (%rax)
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rbp, 0xb0(%rsp)
callq 0x2170
movq %rax, %r13
leaq (%r14,%rbx), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq 0xa8(%rsp), %rdx
callq 0x55a0
movq %rax, %r15
testq %r14, %r14
je 0x34ff
movb $0x0, (%rbp,%r15)
addq %rbx, %r15
callq 0x2170
movq %rax, %rbp
subq %r13, %rax
addq %rax, 0xa0(%rsp)
movq %r15, 0x30(%rsp)
testq %r14, %r14
je 0x3555
movq %r15, %r12
sarq $0x3f, %r12
andq %r15, %r12
movq %r15, %rax
testq %rax, %rax
jle 0x3540
cmpb $0x3e, (%r14,%rax)
je 0x353d
decq %rax
jmp 0x352c
movq %rax, %r12
movq %rax, 0x30(%rsp)
testq %r12, %r12
je 0x3555
cmpb $0x3e, (%r14,%r12)
jne 0x38ff
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
leaq 0x48(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x31c2
movq 0x48(%rsp), %r14
movq 0x30(%rsp), %r9
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %rbx, %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
callq 0x29c6
andl $0x0, 0x20(%rsp)
movl $0x1, 0x24(%rsp)
cmpq $0x0, 0x238(%rbx)
je 0x35c0
andl $0x0, 0x24(%rsp)
leaq 0x24(%rsp), %rdi
leaq 0x20(%rsp), %rsi
pushq $0x1
popq %rdx
movq 0x59ec(%rip), %rcx      # 0x8fc0
movq 0x599d(%rip), %r8       # 0x8f78
movq 0x59b6(%rip), %r9       # 0x8f98
callq 0x22d0
testq %r14, %r14
je 0x361f
subq %r12, %r15
addq %r14, %r12
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3612
movb (%r12,%rcx), %dl
movb %dl, (%r14,%rcx)
incq %rcx
jmp 0x3600
movb $0x0, (%r14,%r15)
movq %r15, %rbx
xorl %r12d, %r12d
jmp 0x3622
movq %r15, %rbx
callq 0x2170
movq %rax, %rcx
subq %rbp, %rcx
addq %rcx, 0x98(%rsp)
subq %r13, %rax
addq %rax, 0x90(%rsp)
movq 0xb0(%rsp), %rbp
incq %rbp
movq 0x28(%rsp), %r15
cmpl $0x0, (%r15)
jne 0x367f
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
leaq 0x2d08(%rip), %rsi      # 0x6370
xorl %edx, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
movq %r13, %rsi
callq 0x51d4
movl 0x20(%rsp), %eax
cmpl 0x4(%r15), %eax
jl 0x34ca
subq %r12, %rbx
testq %r14, %r14
je 0x36bd
addq %r14, %r12
xorl %eax, %eax
testq %rbx, %rbx
movl $0x0, %ecx
cmovgq %rbx, %rcx
cmpq %rax, %rcx
je 0x36b8
movb (%r12,%rax), %dl
movb %dl, (%r14,%rax)
incq %rax
jmp 0x36a6
movb $0x0, (%r14,%rbx)
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
movq %rbx, %r9
callq 0x29c6
movq 0x58bb(%rip), %r13      # 0x8fb0
movq 0x587c(%rip), %r15      # 0x8f78
movq 0x5895(%rip), %r12      # 0x8f98
leaq 0xb8(%rsp), %rdi
leaq 0x30(%rsp), %rbx
pushq $0x1
popq %rbp
movq %rbx, %rsi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
leaq 0xa0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
leaq 0x78(%rsp), %rsi
leaq 0x58(%rsp), %rdi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
leaq 0x68(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movl %ebp, %edx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x22d0
movq (%rbx), %rax
movq 0x28(%rsp), %rcx
movslq 0x4(%rcx), %rcx
cqto
idivq %rcx
movq %rax, (%rbx)
leaq 0x88(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0x78(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0x80(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0x70(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0x68(%rsp), %rdi
movq (%rdi), %rax
cqto
idivq %rcx
leaq 0xc8(%rsp), %rsi
andq $0x0, (%rsi)
movq %rax, (%rdi)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
leaq 0xc0(%rsp), %rdi
movq %rax, (%rdi)
movq 0x5760(%rip), %rcx      # 0x8fa8
movl %ebp, %edx
movq %r15, %r8
movq 0x28(%rsp), %rbx
movq %r12, %r9
callq 0x22d0
cmpl $0x0, (%rbx)
jne 0x38d5
cvtsi2ssq 0x30(%rsp), %xmm0
movss 0x2796(%rip), %xmm5     # 0x6004
cvtsi2ssq 0x88(%rsp), %xmm1
divss %xmm5, %xmm0
cvtss2sd %xmm0, %xmm0
divss %xmm5, %xmm1
cvtss2sd %xmm1, %xmm1
cvtsi2ssq 0x80(%rsp), %xmm2
divss %xmm5, %xmm2
cvtss2sd %xmm2, %xmm2
cvtsi2ssq 0x78(%rsp), %xmm3
divss %xmm5, %xmm3
cvtsi2ssq 0x70(%rsp), %xmm4
cvtss2sd %xmm3, %xmm3
cvtsi2ssq 0x68(%rsp), %xmm6
divss %xmm5, %xmm4
cvtss2sd %xmm4, %xmm4
divss %xmm5, %xmm6
cvtss2sd %xmm6, %xmm5
leaq 0x2ab7(%rip), %rdi      # 0x6385
movb $0x6, %al
callq 0x2040
movq %rbx, %rdi
callq 0x5736
testq %r14, %r14
je 0x38ea
movq %r14, %rdi
callq 0x22f0
pushq $0x1
popq %rax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29f7(%rip), %rdi      # 0x62fd
leaq 0x2736(%rip), %rsi      # 0x6043
leaq 0x2a0d(%rip), %rcx      # 0x6321
movl $0x1e6, %edx            # imm = 0x1E6
callq 0x2160