// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rcx, 0x60(%rsp)
movq %rdx, %r12
movq %rsi, %rbx
xorl %r14d, %r14d
movq %r14, 0xa0(%rsp)
movq %r14, 0x48(%rsp)
movq %r14, 0x98(%rsp)
movq %r14, 0x40(%rsp)
movslq 0x4(%rdx), %rax
movslq 0x278(%rdx), %rsi
cqto
idivq %rsi
movq %rax, %rcx
movq %rax, %rdx
shlq $0xe, %rdx
movl $0x2000000, %eax        # imm = 0x2000000
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %rdx, 0x50(%rsp)
movq %rdi, 0x68(%rsp)
movq %r14, 0x38(%rsp)
movl (%r12), %eax
cltd
idivl %esi
testl %edx, %edx
jne 0x3597
movq 0x50(%rsp), %rax
leaq 0x2(,%rax,2), %rax
testq %rcx, %rcx
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movq %rax, 0x38(%rsp)
callq 0x2180
movq %rax, %r15
movq %r12, %rdi
movq %rbx, 0x58(%rsp)
movq %rbx, %rsi
callq 0x55f4
movq %r12, %rdi
callq 0x5690
callq 0x2180
subq %r15, %rax
movq %rax, 0xb8(%rsp)
movq %rax, 0x90(%rsp)
leaq 0x2c(%rsp), %rax
movl $0x1, (%rax)
leaq 0x28(%rsp), %rax
movl $0x0, (%rax)
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r12, %r15
movq %r12, 0x30(%rsp)
movq %r13, 0xa8(%rsp)
callq 0x2180
movq %rax, %r12
leaq (%r14,%rbx), %r13
movq %r15, %rdi
movq %r13, %rsi
movq 0x50(%rsp), %rdx
callq 0x591c
movq %rax, %r15
testq %r14, %r14
je 0x3626
movb $0x0, (%r13,%r15)
addq %rbx, %r15
callq 0x2180
movq %rax, %r13
subq %r12, %rax
addq %rax, 0xa0(%rsp)
movq %r15, 0x20(%rsp)
testq %r14, %r14
je 0x3687
movq %r15, %rbp
testq %r15, %r15
jle 0x3672
movq %r15, %rbp
cmpb $0x3e, (%r14,%rbp)
je 0x3672
leaq -0x1(%rbp), %rax
cmpq $0x1, %rbp
movq %rax, %rbp
jg 0x3651
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
jmp 0x3687
movq %rbp, 0x20(%rsp)
testq %rbp, %rbp
je 0x3687
cmpb $0x3e, (%r14,%rbp)
jne 0x3a41
movq 0x30(%rsp), %rbx
movq %rbx, %rdx
leaq 0x38(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x32d0
movq 0x38(%rsp), %r14
movq 0x20(%rsp), %r9
leaq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %rbx, %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
callq 0x2a1a
movl $0x1, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
cmpq $0x0, 0x238(%rbx)
je 0x36f8
movl $0x0, 0x2c(%rsp)
leaq 0x2c(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movl $0x1, %edx
movq 0x58b2(%rip), %rcx      # 0x8fc0
movq 0x585b(%rip), %r8       # 0x8f70
movq 0x5874(%rip), %r9       # 0x8f90
callq 0x2310
testq %r14, %r14
je 0x374c
subq %rbp, %r15
jle 0x3740
addq %r14, %rbp
xorl %eax, %eax
movb (%rbp,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
cmpq %r15, %rax
jl 0x3730
movb $0x0, (%r14,%r15)
movq %r15, %rbx
xorl %ebp, %ebp
jmp 0x374f
movq %r15, %rbx
callq 0x2180
movq %rax, %rcx
subq %r13, %rcx
addq %rcx, 0x98(%rsp)
subq %r12, %rax
addq %rax, 0x90(%rsp)
movq 0xa8(%rsp), %r13
incq %r13
movq 0x30(%rsp), %r15
cmpl $0x0, (%r15)
jne 0x37ac
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
leaq 0x2bdb(%rip), %rsi      # 0x6370
xorl %edx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2070
movq %r15, %rdi
movq %r12, %rsi
callq 0x54fa
movl 0x28(%rsp), %eax
cmpl 0x4(%r15), %eax
jl 0x35f4
movq %rbx, %r9
subq %rbp, %r9
testq %r14, %r14
je 0x37e2
testq %r9, %r9
jle 0x37dd
movq %r14, %rax
movb (%rax,%rbp), %cl
movb %cl, (%rax)
decq %rbx
incq %rax
cmpq %rbx, %rbp
jne 0x37cd
movb $0x0, (%r14,%r9)
leaq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq %r15, %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
callq 0x2a1a
movq 0x5796(%rip), %rbp      # 0x8fb0
movq 0x574f(%rip), %r15      # 0x8f70
movq 0x5768(%rip), %r12      # 0x8f90
leaq 0xb8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0xa0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0xb0(%rsp), %r13
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0x70(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movl $0x1, %edx
movq %rbp, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x2310
leaq 0x20(%rsp), %rsi
movq (%rsi), %rax
movq 0x30(%rsp), %rcx
movslq 0x4(%rcx), %rcx
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0x88(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
movq (%r13), %rax
cqto
idivq %rcx
movq %rax, (%r13)
leaq 0x80(%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 0x70(%rsp), %rsi
movq (%rsi), %rax
cqto
idivq %rcx
movq %rax, (%rsi)
leaq 0xc8(%rsp), %rsi
movq $0x0, (%rsi)
movq 0x18(%rbx), %rax
leaq 0xc0(%rsp), %rdi
movq %rax, (%rdi)
movq 0x5629(%rip), %rcx      # 0x8fa8
movl $0x1, %edx
movq %r15, %r8
movq 0x30(%rsp), %rbx
movq %r12, %r9
callq 0x2310
cmpl $0x0, (%rbx)
jne 0x3a15
cvtsi2ssq 0x20(%rsp), %xmm0
movss 0x265c(%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 0xb0(%rsp), %xmm3
divss %xmm5, %xmm3
cvtsi2ssq 0x78(%rsp), %xmm4
cvtss2sd %xmm3, %xmm3
cvtsi2ssq 0x70(%rsp), %xmm6
divss %xmm5, %xmm4
cvtss2sd %xmm4, %xmm4
divss %xmm5, %xmm6
xorps %xmm5, %xmm5
cvtss2sd %xmm6, %xmm5
leaq 0x2977(%rip), %rdi      # 0x6385
movb $0x6, %al
callq 0x2050
movq %rbx, %rdi
callq 0x5ab6
testq %r14, %r14
je 0x3a2a
movq %r14, %rdi
callq 0x2330
movl $0x1, %eax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28b5(%rip), %rdi      # 0x62fd
leaq 0x25f4(%rip), %rsi      # 0x6043
leaq 0x28cb(%rip), %rcx      # 0x6321
movl $0x1e6, %edx            # imm = 0x1E6
callq 0x2170