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

📄 netmpi.cpp

📁 mpi 结合vc编程用模拟退火法算一个最小路径的值
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				else
					RecvTime(&args, &bwdata[n].t, &nzero);
				
				if (!streamopt)
					bwdata[n].variance = t2/TRIALS - t1/TRIALS * t1/TRIALS;
				
			}
			else
			{
				bwdata[n].t = LONGTIME;
				t2 = t1 = 0;
				for (i = 0; i < TRIALS; i++)
				{
					if (bNoCache)
					{
						if (bufalign != 0)
						{
							args.buff = memtmp + ((bufalign - ((int)args.buff % bufalign) + bufoffset) % bufalign);
							/* args.buff1 = memtmp1 + ((bufalign - ((int)args.buff1 % bufalign) + bufoffset) % bufalign); */
						}
						else
						{
							args.buff = memtmp;
							/* args.buff1 = memtmp1; */
						}
					}
			
					Sync(&args);
					t0 = When();
					for (j = 0; j < nrepeat; j++)
					{
						if (bHeadToHead)
							SendRecvData(&args);
						else
						{
							RecvData(&args);
							if (!streamopt)
								SendData(&args);
						}
						if (bNoCache)
						{
							args.buff += args.bufflen;
							/* args.buff1 += args.bufflen; */
						}
					}
					t = (When() - t0)/((1 + !streamopt) * nrepeat);
					
					if (streamopt)
					{
						t2 += t*t;
						t1 += t;
						bwdata[n].t = MIN(bwdata[n].t, t);
					}
				}
				if (streamopt)
					SendTime(&args, &bwdata[n].t, &nzero);
				else
					RecvTime(&args, &bwdata[n].t, &nzero);
				
				if (streamopt)
					bwdata[n].variance = t2/TRIALS - t1/TRIALS * t1/TRIALS;
				
			}
			tlast = bwdata[n].t;
			bwdata[n].bits = args.bufflen * CHARSIZE;
			bwdata[n].bps = bwdata[n].bits / (bwdata[n].t * 1024 * 1024);
			bwdata[n].repeat = nrepeat;
			
			if (args.tr)
			{
			    if (bSavePert)
			    {
				/* fprintf(out,"%lf\t%lf\t%d\t%d\t%lf\n", bwdata[n].t, bwdata[n].bps,
				    bwdata[n].bits, bwdata[n].bits / 8, bwdata[n].variance); */
				fprintf(out,"%d\t%lf\t%lf\n", bwdata[n].bits / 8, bwdata[n].bps, bwdata[n].t);
				fflush(out);
			    }
			}
			
			free(memtmp);
			free(memtmp1);
			
			if (args.tr && printopt)
			{
				fprintf(stdout," %6.2lf Mbps in %lf sec\n", bwdata[n].bps, tlast);
				fflush(stdout);
			}
		} /* End of perturbation loop */
		if (!bSavePert && args.tr)
		{
		    /* if we didn't save all of the perturbation loops, find the max and save it */
		    int index = 1;
		    double dmax = bwdata[n-1].bps;
		    for (; ipert > 1; ipert--)
		    {
			if (bwdata[n-ipert].bps > dmax)
			{
			    index = ipert;
			    dmax = bwdata[n-ipert].bps;
			}
		    }
		    fprintf(out,"%d\t%f\t%f\n", bwdata[n-index].bits / 8, bwdata[n-index].bps, bwdata[n-index].t);
		    fflush(out);
		}
	} /* End of main loop  */
	
	if (args.tr)
		fclose(out);
/* THE_END:		 */
	CleanUp(&args);
	delete bwdata;
}


/* Return the current time in seconds, using a double precision number. 	 */
double When()
{
	return MPI_Wtime();
}

int Setup(ArgStruct *p)
{
	int nproc;
	char s[255];
	int len = 255;
	
	MPI_Comm_rank(MPI_COMM_WORLD, &p->prot.iproc);
	MPI_Comm_size(MPI_COMM_WORLD, &nproc);
	
	MPI_Get_processor_name(s, &len);
	printf("%d: %s\n", p->prot.iproc, s);
	fflush(stdout);
	
	if (p->prot.iproc == 0)
		p->prot.nbor = 1;
	else
		p->prot.nbor = 0;
	
	if (nproc < 2)
	{
		printf("Need two processes\n");
		printf("nproc: %i\n", nproc);
		exit(-2);
	}
	
	if (p->prot.iproc == 0)
		p->tr = 1;
	else
		p->tr = 0;
	return 1;	
}	

void Sync(ArgStruct *p)
{
	char ch;
	MPI_Status status;
	if (p->tr)
	{
		MPI_Send(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
		MPI_Recv(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);
		MPI_Send(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
	}
	else
	{
		MPI_Recv(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);
		MPI_Send(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
		MPI_Recv(&ch, 1, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);
	}
}

int DetermineLatencyReps(ArgStruct *p)
{
    MPI_Status status;
    double t0, duration = 0;
    int reps = 1;
    int i;

    /* prime the send/receive pipes */
    Sync(p);
    Sync(p);
    Sync(p);

    /* test how long it takes to send n messages 
     * where n = 1, 2, 4, 8, 16, 32, ...
     */
    while ( (duration < 0.1) ||
	    (duration < 0.3 && reps < 1000))
    {
	t0 = When();
	t0 = When();
	t0 = When();
	t0 = When();
	for (i=0; i<reps; i++)
	{
	    Sync(p);
	}
	duration = When() - t0;
	reps = reps * 2;

	/* use duration from the root only */
	if (p->prot.iproc == 0)
	    MPI_Send(&duration, 1, MPI_DOUBLE, p->prot.nbor, 2, MPI_COMM_WORLD);
	else
	    MPI_Recv(&duration, 1, MPI_DOUBLE, p->prot.nbor, 2, MPI_COMM_WORLD, &status);
    }

    return reps;
}

double TestLatency(ArgStruct *p)
{
    double latency, t0;
    int i;

    g_LATENCYREPS = DetermineLatencyReps(p);
    if (g_LATENCYREPS < 1024 && p->prot.iproc == 0)
    {
	printf("Using %d reps to determine latency\n", g_LATENCYREPS);
	fflush(stdout);
    }

    p->bufflen = 1;
    p->buff = (char *)malloc(p->bufflen);
    p->buff1 = (char *)malloc(p->bufflen);
    Sync(p);
    t0 = When();
    t0 = When();
    t0 = When();
    t0 = When();
    for (i = 0; i < g_LATENCYREPS; i++)
    {
	if (p->tr)
	{
	    SendData(p);
	    RecvData(p);
	}
	else
	{
	    RecvData(p);
	    SendData(p);
	}
    }
    latency = (When() - t0)/(2 * g_LATENCYREPS);
    free(p->buff);
    free(p->buff1);

    return latency;
}

double TestSyncTime(ArgStruct *p)
{
    double synctime, t0;
    int i;

    t0 = When();
    t0 = When();
    t0 = When();
    t0 = When();
    t0 = When();
    t0 = When();
    for (i = 0; i < g_LATENCYREPS; i++)
	Sync(p);
    synctime = (When() - t0)/g_LATENCYREPS;

    return synctime;
}

void SendRecvData(ArgStruct *p)
{
	MPI_Status status;
	
	MPI_Sendrecv(p->buff, p->bufflen, MPI_BYTE, p->prot.nbor, 1, p->buff1, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);

	/*
	//MPI_Request request;
	//MPI_Irecv(p->buff1, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &request);
	//MPI_Send(p->buff, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
	//MPI_Wait(&request, &status);

	//MPI_Send(p->buff, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
	//MPI_Recv(p->buff1, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);
	*/
}

void SendData(ArgStruct *p)
{
	MPI_Send(p->buff, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD);
}

void RecvData(ArgStruct *p)
{
	MPI_Status status;
	MPI_Recv(p->buff1, p->bufflen, MPI_BYTE, p->prot.nbor, 1, MPI_COMM_WORLD, &status);
}


void SendTime(ArgStruct *p, double *t, int *rpt)
{
	if (*rpt > 0)
		MPI_Send(rpt, 1, MPI_INT, p->prot.nbor, 2, MPI_COMM_WORLD);
	else
		MPI_Send(t, 1, MPI_DOUBLE, p->prot.nbor, 2, MPI_COMM_WORLD);
}

void RecvTime(ArgStruct *p, double *t, int *rpt)
{
	MPI_Status status;
	if (*rpt > 0)
		MPI_Recv(rpt, 1, MPI_INT, p->prot.nbor, 2, MPI_COMM_WORLD, &status);
	else
		MPI_Recv(t, 1, MPI_DOUBLE, p->prot.nbor, 2, MPI_COMM_WORLD, &status);
}

int Establish(ArgStruct *p)
{
	return 1;
}

int  CleanUp(ArgStruct *p)
{
	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Finalize();
	return 1;
}

⌨️ 快捷键说明

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