// File: Mengjintao[P]SWAPCounter/src/mympi.cpp
// Function: MPIEnviroment::File_locate()
void MPIEnviroment::File_locate()
{
	clock_t startTime, endTime;
	startTime = clock();		

        size = -1;
        if(rank==0)	MPI_File_get_size(cFile, &size);
	MPI_Bcast(&size,1,MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD);	
	printf("Proc:%d size of file is: %lld ...\n", rank,  size);
	fflush(stdout);

	if(rank%this->groupProc)	{
		start_pos = end_pos = 0;
		datasize = 0;
//        	printf("Proc:%d file start at %llu and end at %llu...\n", rank,  start_pos, end_pos);
		return;
	}
        unsigned long long step = (size-1)/(nprocs/groupProc)+1;
	int groupRank = rank/groupProc;
	//set start position and end position for file pointer
	start_pos = groupRank*step;
	start_pos = start_pos<size?start_pos:size;
	end_pos   = (groupRank+1)*step;
	end_pos   = end_pos<size?end_pos:size;

	//Find the start position for one read.
	char str[PreReadLen+2];
	unsigned long long  preLen = size-start_pos;
	if(preLen>PreReadLen)	preLen=PreReadLen;	
	MPI_File_read_at(cFile, start_pos, str, preLen, MPI_CHAR, &status);
	unsigned long long i=0;	
	while(str[i]!='>' && start_pos<size && i<preLen) {i++; start_pos++;}
	assert(str[i]=='>'||start_pos==size||i==preLen);
	
	//Find the end position for one read.
	preLen = min(size-end_pos, (long long int) PreReadLen);
	MPI_File_read_at(cFile,end_pos,str,preLen, MPI_CHAR, &status);
	i=0;
        while(str[i]!='>' && end_pos<size && i<preLen) {i++; end_pos++;}

	datasize = end_pos-start_pos;
//        printf("Proc:%d file start at %llu and end at %llu...\n", rank,  start_pos, end_pos);
	read_offset = start_pos;

	endTime = clock();
	elapsedTime += endTime-startTime;
	locateTime  += endTime-startTime;	
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x28(%rsp)
callq 0x83e0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x198(%rsp)
movq $-0x1, 0x220(%rax)
cmpl $0x0, (%rax)
jne 0x15579
movq 0x28(%rsp), %rsi
movq 0x218(%rsi), %rdi
addq $0x220, %rsi            # imm = 0x220
callq 0x85d0
movq 0x28(%rsp), %rdi
addq $0x220, %rdi            # imm = 0x220
movl $0x1, %esi
movq 0x7a5f(%rip), %rdx      # 0x1cff0
xorl %ecx, %ecx
movq 0x79f6(%rip), %r8       # 0x1cf90
callq 0x80d0
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x220(%rax), %rdx
leaq 0x1897(%rip), %rdi      # 0x16e4b
movb $0x0, %al
callq 0x8040
movq 0x79ae(%rip), %rax      # 0x1cf70
movq (%rax), %rdi
callq 0x87b0
movq 0x28(%rsp), %rcx
movl (%rcx), %eax
cltd
idivl 0x278(%rcx)
cmpl $0x0, %edx
je 0x15608
movq 0x28(%rsp), %rax
movq $0x0, 0x230(%rax)
movq $0x0, 0x228(%rax)
movq $0x0, 0x238(%rax)
jmp 0x159ae
movq 0x28(%rsp), %rcx
movq 0x220(%rcx), %rax
subq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x4(%rcx), %eax
cltd
idivl 0x278(%rcx)
movl %eax, %edx
movq 0x20(%rsp), %rax
movslq %edx, %rsi
cqto
idivq %rsi
addq $0x1, %rax
movq %rax, 0x188(%rsp)
movl (%rcx), %eax
cltd
idivl 0x278(%rcx)
movl %eax, 0x184(%rsp)
movslq 0x184(%rsp), %rax
imulq 0x188(%rsp), %rax
movq %rax, 0x228(%rcx)
movq 0x228(%rcx), %rax
cmpq 0x220(%rcx), %rax
jge 0x1568d
movq 0x28(%rsp), %rax
movq 0x228(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x1569e
movq 0x28(%rsp), %rax
movq 0x220(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rax, 0x228(%rcx)
movl 0x184(%rsp), %eax
addl $0x1, %eax
cltq
imulq 0x188(%rsp), %rax
movq %rax, 0x230(%rcx)
movq 0x230(%rcx), %rax
cmpq 0x220(%rcx), %rax
jge 0x156ee
movq 0x28(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x156ff
movq 0x28(%rsp), %rax
movq 0x220(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rax, 0x230(%rcx)
movq 0x220(%rcx), %rax
subq 0x228(%rcx), %rax
movq %rax, 0x48(%rsp)
cmpq $0x12c, 0x48(%rsp)      # imm = 0x12C
jbe 0x15737
movq $0x12c, 0x48(%rsp)      # imm = 0x12C
movq 0x28(%rsp), %r9
movq 0x218(%r9), %rdi
movq 0x228(%r9), %rsi
leaq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rax
movl %eax, %ecx
addq $0x260, %r9             # imm = 0x260
movq 0x7814(%rip), %r8       # 0x1cf78
callq 0x8470
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
movsbl 0x50(%rsp,%rax), %ecx
xorl %eax, %eax
cmpl $0x3e, %ecx
movb %al, 0xf(%rsp)
je 0x157b3
movq 0x28(%rsp), %rdx
movq 0x228(%rdx), %rcx
xorl %eax, %eax
cmpq 0x220(%rdx), %rcx
movb %al, 0xf(%rsp)
jge 0x157b3
movq 0x40(%rsp), %rax
cmpq 0x48(%rsp), %rax
setb %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x157bd
jmp 0x157e4
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movq 0x228(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x228(%rax)
jmp 0x15772
movq 0x40(%rsp), %rax
movsbl 0x50(%rsp,%rax), %ecx
movb $0x1, %al
cmpl $0x3e, %ecx
movb %al, 0xe(%rsp)
je 0x15825
movq 0x28(%rsp), %rdx
movq 0x228(%rdx), %rcx
movb $0x1, %al
cmpq 0x220(%rdx), %rcx
movb %al, 0xe(%rsp)
je 0x15825
movq 0x40(%rsp), %rax
cmpq 0x48(%rsp), %rax
sete %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x1582f
jmp 0x15831
jmp 0x15850
leaq 0x1636(%rip), %rdi      # 0x16e6e
leaq 0x15a0(%rip), %rsi      # 0x16ddf
movl $0x63, %edx
leaq 0x164b(%rip), %rcx      # 0x16e96
callq 0x83d0
movq 0x28(%rsp), %rcx
movq 0x220(%rcx), %rax
subq 0x230(%rcx), %rax
movq %rax, 0x38(%rsp)
movq $0x12c, 0x30(%rsp)      # imm = 0x12C
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xd510
movq 0x28(%rsp), %r9
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x218(%r9), %rdi
movq 0x230(%r9), %rsi
leaq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rax
movl %eax, %ecx
addq $0x260, %r9             # imm = 0x260
movq 0x76c3(%rip), %r8       # 0x1cf78
callq 0x8470
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
movsbl 0x50(%rsp,%rax), %ecx
xorl %eax, %eax
cmpl $0x3e, %ecx
movb %al, 0xd(%rsp)
je 0x15904
movq 0x28(%rsp), %rdx
movq 0x230(%rdx), %rcx
xorl %eax, %eax
cmpq 0x220(%rdx), %rcx
movb %al, 0xd(%rsp)
jge 0x15904
movq 0x40(%rsp), %rax
cmpq 0x48(%rsp), %rax
setb %al
movb %al, 0xd(%rsp)
movb 0xd(%rsp), %al
testb $0x1, %al
jne 0x1590e
jmp 0x15935
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movq 0x230(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x230(%rax)
jmp 0x158c3
movq 0x28(%rsp), %rax
movq 0x230(%rax), %rcx
subq 0x228(%rax), %rcx
movq %rcx, 0x238(%rax)
movq 0x228(%rax), %rcx
movq %rcx, 0x240(%rax)
callq 0x83e0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x190(%rsp)
movq 0x190(%rsp), %rcx
subq 0x198(%rsp), %rcx
addq 0x248(%rax), %rcx
movq %rcx, 0x248(%rax)
movq 0x190(%rsp), %rcx
subq 0x198(%rsp), %rcx
addq 0x250(%rax), %rcx
movq %rcx, 0x250(%rax)
addq $0x1a8, %rsp            # imm = 0x1A8
retq
nopw %cs:(%rax,%rax)