⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pair.c

📁 mpi 结合vc编程用模拟退火法算一个最小路径的值
💻 C
📖 第 1 页 / 共 4 页
字号:

double round_trip_vector(int reps, int len, PairData ctx)
{
  double elapsed_time;
  int  i, to = ctx->destination, from = ctx->source;
  int  recv_from;
  unsigned datalen;
  double *rbuffer,*sbuffer;
  double t0, t1;
  MPI_Datatype vec, types[2];
  int          blens[2];
  MPI_Aint     displs[2];
  MPI_Status   status;
  MPI_Comm     comm;
  int          alloc_len;

  /* Adjust len to be in bytes */
  len = len / sizeof(double);
  alloc_len = len;
  if (len == 0) alloc_len++;

  comm = MPI_COMM_WORLD;
  blens[0] = 1; displs[0] = 0; types[0] = MPI_DOUBLE;
  blens[1] = 1; displs[1] = VectorStride * sizeof(double); types[1] = MPI_UB;
  MPI_Type_struct( 2, blens, displs, types, &vec );
  MPI_Type_commit( &vec );

  datalen = VectorStride * alloc_len * sizeof(double);
  sbuffer = (double *)malloc(datalen);
  rbuffer = (double *)malloc(datalen);
  if (!sbuffer)return 0;
  if (!rbuffer)return 0;
  memset( sbuffer, 0, datalen );
  memset( rbuffer, 0, datalen );

  SetupTest( from );
  ConfirmTest( reps, len, ctx );
  elapsed_time = 0;
  if(ctx->is_master){
    recv_from = MPI_ANY_SOURCE;
    if (source_type == SpecifiedSource) recv_from = to;
    MPI_Recv( rbuffer, len, vec, recv_from, 0, comm, &status );
    t0=MPI_Wtime();
    for(i=0;i<reps;i++){
      MPI_Send( sbuffer, len, vec, to, MSG_TAG(i), comm );
      MPI_Recv( rbuffer, len, vec, recv_from, MSG_TAG(i), comm, &status );
      }
    t1=MPI_Wtime();
    elapsed_time = t1 - t0;
    }

  if(ctx->is_slave){
    recv_from = MPI_ANY_SOURCE;
    if (source_type == SpecifiedSource) recv_from = to;
    MPI_Send( sbuffer, len, vec, from, 0, comm );
    for(i=0;i<reps;i++){
	MPI_Recv( rbuffer, len, vec, recv_from, MSG_TAG(i), comm, &status );
	MPI_Send( sbuffer, len, vec, to, MSG_TAG(i), comm );
	}
    }

  FinishTest();
  free(sbuffer);
  free(rbuffer);
  MPI_Type_free( &vec );
  return(elapsed_time);
}

double round_trip_vectortype( int reps, int len, PairData ctx)
{
  double elapsed_time;
  int  i, to = ctx->destination, from = ctx->source;
  int  recv_from;
  unsigned datalen;
  double *rbuffer,*sbuffer;
  double t0, t1;
  MPI_Datatype vec;
  MPI_Status   status;
  MPI_Comm     comm;
  int          alloc_len;

  /* Adjust len to be in doubles */
  len = len / sizeof(double);
  alloc_len = len;
  if (len == 0) alloc_len++;

  comm = MPI_COMM_WORLD;
  MPI_Type_vector( len, 1, VectorStride, MPI_DOUBLE, &vec );
  MPI_Type_commit( &vec );

  datalen = VectorStride * alloc_len * sizeof(double);
  sbuffer = (double *)malloc(datalen);
  rbuffer = (double *)malloc(datalen);
  if (!sbuffer)return 0;
  if (!rbuffer)return 0;
  memset( sbuffer, 0, datalen );
  memset( rbuffer, 0, datalen );

  SetupTest( from );
  ConfirmTest( reps, len, ctx );
  elapsed_time = 0;
  if(ctx->is_master){
    recv_from = MPI_ANY_SOURCE;
    if (source_type == SpecifiedSource) recv_from = to;
    MPI_Recv( rbuffer, 1, vec, recv_from, 0, comm, &status );
    t0=MPI_Wtime();
    for(i=0;i<reps;i++){
      MPI_Send( sbuffer, 1, vec, to, MSG_TAG(i), comm );
      MPI_Recv( rbuffer, 1, vec, recv_from, MSG_TAG(i), comm, &status );
      }
    t1=MPI_Wtime();
    elapsed_time = t1 -t0;
    }

  if(ctx->is_slave){
    recv_from = MPI_ANY_SOURCE;
    if (source_type == SpecifiedSource) recv_from = to;
    MPI_Send( sbuffer, 1, vec, from, 0, comm );
    for(i=0;i<reps;i++){
	MPI_Recv( rbuffer, 1, vec, recv_from, MSG_TAG(i), comm, &status );
	MPI_Send( sbuffer, 1, vec, to, MSG_TAG(i), comm );
	}
    }

  FinishTest();
  free(sbuffer );
  free(rbuffer );
  MPI_Type_free( &vec );
  return(elapsed_time);
}
/*
    These versions try NOT to operate out of cache; rather, then send/receive
    into a moving window.
 */
/* 
   Blocking round trip (always unidirectional) 
 */
double round_trip_nc_sync( int reps, int len, PairData ctx)
{
    double elapsed_time;
    int  i, to = ctx->destination, from = ctx->source;
    int  recv_from;
    char *rbuffer,*sbuffer, *rp, *sp, *rlast, *slast;
    MPI_Status status;
    double t0, t1;

    sbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    slast   = sbuffer + 2 * CacheSize - len;
    rbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    rlast   = rbuffer + 2 * CacheSize - len;
    if (!sbuffer || !rbuffer) {
	fprintf( stderr, "Could not allocate %d bytes\n", 4 * CacheSize );
	exit(1 );
    }
    memset( sbuffer, 0, 2*CacheSize );
    memset( rbuffer, 0, 2*CacheSize );
    sp = sbuffer;
    rp = rbuffer;

    SetupTest( from );
    ConfirmTest( reps, len, ctx );
    elapsed_time = 0;
    if(ctx->is_master){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Recv(rbuffer,len,MPI_BYTE,recv_from,0,MPI_COMM_WORLD,&status);
	t0=MPI_Wtime();
	for(i=0;i<reps;i++){
	    MPI_Send(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    MPI_Recv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		     MPI_COMM_WORLD,&status);
	    sp += len;
	    rp += len;
	    if (sp > slast) sp = sbuffer;
	    if (rp > rlast) rp = rbuffer;
	}
	t1=MPI_Wtime();
	elapsed_time = t1 -t0;
    }

    if(ctx->is_slave){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Send(sbuffer,len,MPI_BYTE,from,0,MPI_COMM_WORLD);
	for(i=0;i<reps;i++){
	    MPI_Recv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		     MPI_COMM_WORLD,&status);
	    MPI_Send(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    sp += len;
	    rp += len;
	    if (sp > slast) sp = sbuffer;
	    if (rp > rlast) rp = rbuffer;
	}
    }

    FinishTest();
    free(sbuffer );
    free(rbuffer );
    return(elapsed_time);
}

/* 
   Ready-receiver round trip
 */
double round_trip_nc_force( int reps, int len, PairData ctx)
{
    double elapsed_time;
    int  i, to = ctx->destination, from = ctx->source;
    int  recv_from;
    char *rbuffer,*sbuffer, *rp, *sp, *rlast, *slast;
    double t0, t1;
    MPI_Request rid;
    MPI_Status  status;

    sbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    slast   = sbuffer + 2 * CacheSize - len;
    rbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    rlast   = rbuffer + 2 * CacheSize - len;
    if (!sbuffer || !rbuffer) {
	fprintf( stderr, "Could not allocate %d bytes\n", 4 * CacheSize );
	exit(1 );
    }
    sp = sbuffer;
    rp = rbuffer;
    memset( sbuffer, 0, 2*CacheSize );
    memset( rbuffer, 0, 2*CacheSize );

    SetupTest( from );
    ConfirmTest( reps, len, ctx );
    elapsed_time = 0;
    if(ctx->is_master){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Recv(rbuffer,len,MPI_BYTE,recv_from,0,MPI_COMM_WORLD,&status);
	t0=MPI_Wtime();
	for(i=0;i<reps;i++){
	    MPI_Irecv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		      MPI_COMM_WORLD,&(rid));
	    MPI_Rsend(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    MPI_Wait(&(rid),&status);
	    sp += len;
	    rp += len;
	    if (sp > slast) sp = sbuffer;
	    if (rp > rlast) rp = rbuffer;
	}
	t1=MPI_Wtime();
	elapsed_time = t1 -t0;
    }

    if(ctx->is_slave){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Irecv(rbuffer,len,MPI_BYTE,recv_from,MSG_TAG(i),
		  MPI_COMM_WORLD,&(rid));
	MPI_Send(sbuffer,len,MPI_BYTE,from,0,MPI_COMM_WORLD);
	for(i=0;i<reps-1;i++){
	    MPI_Wait(&(rid),&status);
	    rp += len;
	    if (rp > rlast) rp = rbuffer;
	    MPI_Irecv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		      MPI_COMM_WORLD,&(rid));
	    MPI_Rsend(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    sp += len;
	    if (sp > slast) sp = sbuffer;
	}
	MPI_Wait(&(rid),&status);
	MPI_Rsend(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
    }

    FinishTest();
    free(sbuffer );
    free(rbuffer );
    return(elapsed_time);
}

/* 
   Nonblocking round trip
 */
double round_trip_nc_async( int reps, int len, PairData ctx)
{
    double elapsed_time;
    int  i, to = ctx->destination, from = ctx->source;
    int  recv_from;
    char *rbuffer,*sbuffer, *rp, *sp, *rlast, *slast;
    double t0, t1;
    MPI_Request rid;
    MPI_Status  status;

    sbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    slast   = sbuffer + 2 * CacheSize - len;
    rbuffer = (char *)malloc((unsigned)(2 * CacheSize ));
    rlast   = rbuffer + 2 * CacheSize - len;
    if (!sbuffer || !rbuffer) {
	fprintf( stderr, "Could not allocate %d bytes\n", 4 * CacheSize );
	exit(1 );
    }
    sp = sbuffer;
    rp = rbuffer;
    memset( sbuffer, 0, 2*CacheSize );
    memset( rbuffer, 0, 2*CacheSize );

    SetupTest( from );
    ConfirmTest( reps, len, ctx );
    elapsed_time = 0;
    if(ctx->is_master){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Recv(rbuffer,len,MPI_BYTE,recv_from,0,MPI_COMM_WORLD,&status);
	t0=MPI_Wtime();
	for(i=0;i<reps;i++){
	    MPI_Irecv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		      MPI_COMM_WORLD,&(rid));
	    MPI_Send(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    MPI_Wait(&(rid),&status);
	    sp += len;
	    rp += len;
	    if (sp > slast) sp = sbuffer;
	    if (rp > rlast) rp = rbuffer;
	}
	t1=MPI_Wtime();
	elapsed_time = t1 -t0;
    }

    if(ctx->is_slave){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Irecv(rbuffer,len,MPI_BYTE,recv_from,MSG_TAG(i),
		  MPI_COMM_WORLD,&(rid));
	MPI_Send(sbuffer,len,MPI_BYTE,from,0,MPI_COMM_WORLD);
	for(i=0;i<reps-1;i++){
	    MPI_Wait(&(rid),&status);
	    rp += len;
	    if (rp > rlast) rp = rbuffer;
	    MPI_Irecv(rp,len,MPI_BYTE,recv_from,MSG_TAG(i),
		      MPI_COMM_WORLD,&(rid));
	    MPI_Send(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
	    sp += len;
	    if (sp > slast) sp = sbuffer;
	}
	MPI_Wait(&(rid),&status);
	MPI_Send(sp,len,MPI_BYTE,to,MSG_TAG(i),MPI_COMM_WORLD);
    }

    FinishTest();
    free(sbuffer );
    free(rbuffer );
    return(elapsed_time);
}

#ifdef HAVE_MPI_PUT
double exchange_put( int reps, int len, PairData ctx)
{
    double elapsed_time;
    int  i, to = ctx->destination, from = ctx->source;
    int  recv_from;
    char *sbuffer,*rbuffer;
    double t0, t1;
    MPI_Status status;
    MPI_Win    win;
    int        alloc_len;
  
    alloc_len = len;
    if (alloc_len == 0) alloc_len = sizeof(double);

#if defined(HAVE_SHMALLOC) && !defined(HAVE_MPI_ALLOC_MEM)
    sbuffer = (char *)shmalloc((unsigned)(alloc_len));
    rbuffer = (char *)shmalloc((unsigned)(alloc_len));
#else
    sbuffer = (char *)malloc((unsigned)(alloc_len));
    rbuffer = (char *)malloc((unsigned)(alloc_len));
#endif
    if (!sbuffer || !rbuffer) {
	fprintf( stderr, "Could not allocate %d bytes\n", alloc_len );
	exit(1 );
    }
    memset( sbuffer, 0, alloc_len );
    memset( rbuffer, 0, alloc_len );

    MPI_Win_create( rbuffer, len, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win );

    SetupTest( from );
    ConfirmTest( reps, len, ctx );

    elapsed_time = 0;
    if(ctx->is_master){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Recv(rbuffer,len,MPI_BYTE,recv_from,0,MPI_COMM_WORLD,&status);
	t0=MPI_Wtime();
	for(i=0;i<reps;i++){
	    MPI_Put( sbuffer, len, MPI_BYTE, to, 
		     0, len, MPI_BYTE, win );
	    MPI_Win_fence( 0, win );
	}
	t1 = MPI_Wtime();
	elapsed_time = t1-t0;
    }
    else if(ctx->is_slave){
	recv_from = MPI_ANY_SOURCE;
	if (source_type == SpecifiedSource) recv_from = to;
	MPI_Send(sbuffer,len,MPI_BYTE,from,0,MPI_COMM_WORLD);
	for(i=0;i<reps;i++){
	    MPI_Put( sbuffer, len, MPI_BYTE, from, 
		     0, len, MPI_BYTE, win );
	    MPI_Win_fence( 0, win );
	}
    }
    else {
	for(i=0;i<reps;i++){
	    MPI_Win_fence( 0, win );
	}
    }

    FinishTest();
    MPI_Win_free( &win );
#if defined(HAVE_SHMALLOC) && !defined(HAVE_MPI_ALLOC_MEM)
    shfree( sbuffer );
    shfree( rbuffer );
#else
    free(sbuffer );
    free(rbuffer );
#endif
    return(elapsed_time);
}
double round_trip_put( int reps, int len, PairData ctx)
{
    double elapsed_time;
    int  i, to = ctx->destination, from = ctx->source;
    int  recv_from;
    char *rbuffer,*sbuffer;
    double t0, t1;
    MPI_Win win;
    MPI_Status status;
    int alloc_len;

    alloc_len = len;
    if (alloc_len == 0) alloc_len = sizeof(double);

#if defined(HAVE_SHMALLOC) && !defined(HAVE_MPI_ALLOC_MEM)
    sbuffer = (char *)shmalloc((unsigned)(alloc_len));
    rbuffer = (char *)shmalloc((unsigned)(alloc_len));
#else
    sbuffer = (char *)malloc((unsigned)(alloc_len));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -