void segy_dispatching()
{
struct SegyFile segyfh = segy_read_open(segy_file, 2, 2);
size_t segy_trace_length = 240 + segyfh.ntdata*sizeof(float);
size_t ntrace_per_read = 100000;
size_t trace_buffer_size = ntrace_per_read*segy_trace_length;
DEBUG("buffer_size=%f GB\n", trace_buffer_size/((double)GB));
//-------------------------------------------------------
char *trace_buffer[2];
trace_buffer[0] =(char*)malloc(trace_buffer_size);
trace_buffer[1] =(char*)malloc(trace_buffer_size);
assert(trace_buffer[0]!=NULL && trace_buffer[1]!=NULL);
//--------------------------------------------------------
MPI_Init(0,0);
int nnode;
int rank;
MPI_Comm_size(MPI_COMM_WORLD, &nnode);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_File fh;
int err = MPI_File_open(MPI_COMM_WORLD, segy_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
assert(err==MPI_SUCCESS);
//--------------------------------------
// 节点间数据划分:
int total_trace = segyfh.total_numtrace;
int my_ntrace, my_start_itrace, my_end_itrace;
my_ntrace = (total_trace+nnode-1)/nnode;
my_start_itrace=my_ntrace*rank;
my_end_itrace = my_start_itrace+my_ntrace;
if(my_end_itrace>=total_trace) my_end_itrace=total_trace;
fprintf(stderr, "Node#%d: my_start_itrace = %d, my_end_itrace=%d\n",
rank, my_start_itrace, my_end_itrace);
size_t nleft=my_end_itrace-my_start_itrace;
nleft *=segy_trace_length;
//seek
MPI_Offset off=(MPI_Offset)segy_trace_length*my_start_itrace + 3600UL;
err = MPI_File_seek(fh, off, MPI_SEEK_SET);
assert(err==0);
//--------------------------------------
MPI_Request req;
MPI_Status status;
ssize_t bytes_written;
int count;
int current=0;
int next=1;
int bytes_to_read, bytes_to_write;
bytes_to_read = nleft>trace_buffer_size?trace_buffer_size:nleft;
//iread
err=MPI_File_iread(fh, trace_buffer[current], bytes_to_read, MPI_BYTE, &req);
assert(err==0);
int out_fd = open("/tmp/1.segy", O_CREAT|O_WRONLY|O_TRUNC, 0666);
assert(out_fd>=0);
int loops=0;
size_t bytes_read=0;
size_t total_read=nleft;
double t_start=omp_get_wtime();
while(1)
{
double time0=omp_get_wtime();
//---------------------------------------
//wait
err=MPI_Wait(&req, &status);
assert(err==0);
err = MPI_Get_count(&status, MPI_BYTE,&count);
assert(count==bytes_to_read);
bytes_to_write=count;
nleft-=bytes_to_write;
//-------------------------------------
bytes_to_read = nleft>trace_buffer_size?trace_buffer_size:nleft;
// 双缓冲, iread
err=MPI_File_iread(fh, trace_buffer[next], bytes_to_read, MPI_BYTE, &req);
assert(err==0);
//--------------------------------------
//write
bytes_written = write(out_fd, trace_buffer[current], bytes_to_write);
if(bytes_written != bytes_to_write)
{
perror("write() failed");
assert(0);
}
{
int t=current;
current=next;
next=t;
}
bytes_read+=count;
count/=segy_trace_length;
//---------------------------------------
DEBUG("#%d: ntrace_read = %d(%ld/%ld), time used %f seconds, Elapse %f seconds\n",
loops, count,
(long)bytes_read/segy_trace_length,
(long)total_read/segy_trace_length,
omp_get_wtime()-time0,
omp_get_wtime()-t_start);
DEBUG("=====#%d: %f MB/s\n",
loops, bytes_to_write/(omp_get_wtime()-time0)/MB);
loops++;
if(bytes_to_read<trace_buffer_size) break;
}
//-------------------------------------------
MPI_Wait(&req, &status);
err = MPI_Get_count(&status, MPI_BYTE, &count);
assert(count==bytes_to_read);
bytes_to_write=count;
bytes_written = write(out_fd, trace_buffer[current], bytes_to_write);
if(bytes_written != bytes_to_write)
{
perror("write() failed");
assert(0);
}
DEBUG("Total time used %f seconds\n", omp_get_wtime() - t_start);
//----------------------------------------------
MPI_File_close(&fh);
MPI_Finalize();
segy_file_close(&segyfh);
free(trace_buffer[0]);
free(trace_buffer[1]);
close(out_fd);
}
{
struct SegyFile segyfh = segy_read_open(segy_file, 2, 2);
size_t segy_trace_length = 240 + segyfh.ntdata*sizeof(float);
size_t ntrace_per_read = 100000;
size_t trace_buffer_size = ntrace_per_read*segy_trace_length;
DEBUG("buffer_size=%f GB\n", trace_buffer_size/((double)GB));
//-------------------------------------------------------
char *trace_buffer[2];
trace_buffer[0] =(char*)malloc(trace_buffer_size);
trace_buffer[1] =(char*)malloc(trace_buffer_size);
assert(trace_buffer[0]!=NULL && trace_buffer[1]!=NULL);
//--------------------------------------------------------
MPI_Init(0,0);
int nnode;
int rank;
MPI_Comm_size(MPI_COMM_WORLD, &nnode);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_File fh;
int err = MPI_File_open(MPI_COMM_WORLD, segy_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
assert(err==MPI_SUCCESS);
//--------------------------------------
// 节点间数据划分:
int total_trace = segyfh.total_numtrace;
int my_ntrace, my_start_itrace, my_end_itrace;
my_ntrace = (total_trace+nnode-1)/nnode;
my_start_itrace=my_ntrace*rank;
my_end_itrace = my_start_itrace+my_ntrace;
if(my_end_itrace>=total_trace) my_end_itrace=total_trace;
fprintf(stderr, "Node#%d: my_start_itrace = %d, my_end_itrace=%d\n",
rank, my_start_itrace, my_end_itrace);
size_t nleft=my_end_itrace-my_start_itrace;
nleft *=segy_trace_length;
//seek
MPI_Offset off=(MPI_Offset)segy_trace_length*my_start_itrace + 3600UL;
err = MPI_File_seek(fh, off, MPI_SEEK_SET);
assert(err==0);
//--------------------------------------
MPI_Request req;
MPI_Status status;
ssize_t bytes_written;
int count;
int current=0;
int next=1;
int bytes_to_read, bytes_to_write;
bytes_to_read = nleft>trace_buffer_size?trace_buffer_size:nleft;
//iread
err=MPI_File_iread(fh, trace_buffer[current], bytes_to_read, MPI_BYTE, &req);
assert(err==0);
int out_fd = open("/tmp/1.segy", O_CREAT|O_WRONLY|O_TRUNC, 0666);
assert(out_fd>=0);
int loops=0;
size_t bytes_read=0;
size_t total_read=nleft;
double t_start=omp_get_wtime();
while(1)
{
double time0=omp_get_wtime();
//---------------------------------------
//wait
err=MPI_Wait(&req, &status);
assert(err==0);
err = MPI_Get_count(&status, MPI_BYTE,&count);
assert(count==bytes_to_read);
bytes_to_write=count;
nleft-=bytes_to_write;
//-------------------------------------
bytes_to_read = nleft>trace_buffer_size?trace_buffer_size:nleft;
// 双缓冲, iread
err=MPI_File_iread(fh, trace_buffer[next], bytes_to_read, MPI_BYTE, &req);
assert(err==0);
//--------------------------------------
//write
bytes_written = write(out_fd, trace_buffer[current], bytes_to_write);
if(bytes_written != bytes_to_write)
{
perror("write() failed");
assert(0);
}
{
int t=current;
current=next;
next=t;
}
bytes_read+=count;
count/=segy_trace_length;
//---------------------------------------
DEBUG("#%d: ntrace_read = %d(%ld/%ld), time used %f seconds, Elapse %f seconds\n",
loops, count,
(long)bytes_read/segy_trace_length,
(long)total_read/segy_trace_length,
omp_get_wtime()-time0,
omp_get_wtime()-t_start);
DEBUG("=====#%d: %f MB/s\n",
loops, bytes_to_write/(omp_get_wtime()-time0)/MB);
loops++;
if(bytes_to_read<trace_buffer_size) break;
}
//-------------------------------------------
MPI_Wait(&req, &status);
err = MPI_Get_count(&status, MPI_BYTE, &count);
assert(count==bytes_to_read);
bytes_to_write=count;
bytes_written = write(out_fd, trace_buffer[current], bytes_to_write);
if(bytes_written != bytes_to_write)
{
perror("write() failed");
assert(0);
}
DEBUG("Total time used %f seconds\n", omp_get_wtime() - t_start);
//----------------------------------------------
MPI_File_close(&fh);
MPI_Finalize();
segy_file_close(&segyfh);
free(trace_buffer[0]);
free(trace_buffer[1]);
close(out_fd);
}