// File: /workspace/github_cpp_makefile_star2_O1/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, 0x58(%rsp)
movq %rdx, %r13
movq %rsi, %r12
xorl %esi, %esi
movq %rsi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rsi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movslq 0x4(%rdx), %rax
movslq 0x278(%rdx), %r8
cqto
idivq %r8
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, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl (%r13), %eax
cltd
idivl %r8d
testl %edx, %edx
jne 0x3584
movq 0x50(%rsp), %rax
leaq 0x2(,%rax,2), %rax
testq %rcx, %rcx
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
callq 0x2180
movq %rax, %r14
movq %r13, %rdi
movq %r12, %rsi
callq 0x5540
movq %r13, %rdi
callq 0x55dc
callq 0x2180
subq %r14, %rax
addq %rax, 0xa8(%rsp)
addq %rax, 0x48(%rsp)
leaq 0x24(%rsp), %rax
movl $0x1, (%rax)
leaq 0x20(%rsp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq $0x0, 0x60(%rsp)
movq %r13, 0x28(%rsp)
movq %r12, 0xb0(%rsp)
callq 0x2180
movq %rax, 0x68(%rsp)
movq 0x30(%rsp), %r12
leaq (%r12,%r14), %r15
movq %r13, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %rdx
callq 0x5896
movq %rax, %rbp
testq %r12, %r12
je 0x3615
movb $0x0, (%r15,%rbp)
addq %r14, %rbp
callq 0x2180
movq %rax, %r13
subq 0x68(%rsp), %rax
addq %rax, 0xa0(%rsp)
movq %rbp, 0x18(%rsp)
testq %r12, %r12
je 0x366c
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
jle 0x3657
cmpb $0x3e, (%r12,%rbx)
je 0x3657
leaq -0x1(%rbx), %rax
cmpq $0x1, %rbx
movq %rax, %rbx
jg 0x3641
xorl %ebx, %ebx
movq %rbx, 0x18(%rsp)
testq %rbx, %rbx
je 0x366c
cmpb $0x3e, (%r12,%rbx)
jne 0x3a30
movq 0x28(%rsp), %r14
movq %r14, %rdx
leaq 0x30(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x32f6
movq 0x30(%rsp), %r15
movq 0x18(%rsp), %r9
leaq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
movq 0xb0(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
movq 0x58(%rsp), %rcx
movq %r15, %r8
callq 0x2a3a
movl $0x1, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
cmpq $0x0, 0x238(%r14)
je 0x36e6
movl $0x0, 0x24(%rsp)
leaq 0x24(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x1, %edx
movq 0x58c4(%rip), %rcx      # 0x8fc0
movq 0x586d(%rip), %r8       # 0x8f70
movq 0x5886(%rip), %r9       # 0x8f90
callq 0x2310
testq %r15, %r15
je 0x3739
subq %rbx, %rbp
jle 0x372d
addq %r15, %rbx
xorl %eax, %eax
movb (%rbx,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
cmpq %rbp, %rax
jl 0x371e
movb $0x0, (%r15,%rbp)
movq %rbp, %r14
xorl %ebx, %ebx
jmp 0x373c
movq %rbp, %r14
callq 0x2180
movq %rax, %rcx
subq %r13, %rcx
addq %rcx, 0x90(%rsp)
subq 0x68(%rsp), %rax
addq %rax, 0x48(%rsp)
incq 0x60(%rsp)
movq 0x28(%rsp), %r13
cmpl $0x0, (%r13)
jne 0x3795
leaq 0xd0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x2bf4(%rip), %rsi      # 0x6370
xorl %edx, %edx
movq 0x60(%rsp), %rcx
xorl %eax, %eax
callq 0x2070
movq %r13, %rdi
movq %rbp, %rsi
callq 0x5446
movl 0x20(%rsp), %eax
cmpl 0x4(%r13), %eax
jl 0x35e5
testq %r15, %r15
je 0x37cd
movq %r14, %rax
subq %rbx, %rax
jle 0x37c8
movq %r15, %rcx
movq %r14, %rdx
movb (%rcx,%rbx), %sil
movb %sil, (%rcx)
decq %rdx
incq %rcx
cmpq %rdx, %rbx
jne 0x37b6
movb $0x0, (%r15,%rax)
subq %rbx, %r14
leaq 0x40(%rsp), %rbx
movq %rbx, 0x10(%rsp)
leaq 0x98(%rsp), %rbx
movq %rbx, 0x8(%rsp)
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq 0x58(%rsp), %rcx
movq %r15, %r8
movq %r14, %r9
callq 0x2a3a
movq 0x57a7(%rip), %rbp      # 0x8fb0
movq 0x5760(%rip), %r14      # 0x8f70
movq 0x5779(%rip), %r12      # 0x8f90
leaq 0xa8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0xa0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0xb8(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0x70(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movl $0x1, %edx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
leaq 0x18(%rsp), %rsi
movq (%rsi), %rax
movq 0x28(%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)
movq 0x28(%rsp), %rbx
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 0x38(%rsp), %rax
movq 0x18(%rax), %rax
leaq 0xc0(%rsp), %rdi
movq %rax, (%rdi)
movq 0x5635(%rip), %rcx      # 0x8fa8
movl $0x1, %edx
movq %r14, %r8
movq %r12, %r9
callq 0x2310
cmpl $0x0, (%rbx)
jne 0x3a04
cvtsi2ssq 0x18(%rsp), %xmm0
movss 0x266d(%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 0xb8(%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 0x2988(%rip), %rdi      # 0x6385
movb $0x6, %al
callq 0x2050
movq %rbx, %rdi
callq 0x5a30
testq %r15, %r15
je 0x3a19
movq %r15, %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 0x28c6(%rip), %rdi      # 0x62fd
leaq 0x2605(%rip), %rsi      # 0x6043
leaq 0x28dc(%rip), %rcx      # 0x6321
movl $0x1e6, %edx            # imm = 0x1E6
callq 0x2170
nop