gpt4 book ai didi

c - gettimeofday 会导致 MPI 崩溃吗?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:57 25 4
gpt4 key购买 nike

我有一个 mpi 程序,我正在尝试测量其运行时间。所以我添加了对 gettimeofday 的 2 个调用,这就是一切停止工作的地方。出于某种原因,只有在我第二次调用 gettimeofday 时它才会崩溃。这是我收到的消息:

MPI 应用程序级别 0 在 MPI_Finalize() 之前被终止,信号为 11srun: error: n32: task0: 已退出,退出代码为 245

这是代码

   struct timeval starttime;
struct timeval endtime;

gettimeofday(&starttime, NULL);

int numDarts = 1000000000;
int numWorkers = 2;

char* args[1];
if(argc >= 2)
{
numWorkers = atoi(argv[1]);
}
if(argc >= 3)
numDarts = atoi(argv[2]);

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

printf("world size = %i\n", world_size);
if (world_size != 1)
printf("Top heavy with management\n");

int numDartsWorker = numDarts/numWorkers;
int numDartsMaster = numDarts/numWorkers + (numDarts % numWorkers); //the master computes the leftover
args[0] = malloc(256 * sizeof(char));
sprintf(args[0], "%i", numDartsWorker);
// printf("argument passing to workers: %s\n", args[0]);
/*
* Now spawn the workers. Note that there is a run-time determination
* of what type of worker to spawn, and presumably this calculation must
* be done at run time and cannot be calculated before starting
* the program. If everything is known when the application is
* first started, it is generally better to start them all at once
* in a single MPI_COMM_WORLD.
*/
// printf("About to call MPI_Comm_spawn with %i workers...\n", numWorkers);
int resultLen = 0;

//the master counts as a worker, hence the -1
MPI_Comm_spawn("piworker", args, numWorkers-1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
&everyone, MPI_ERRCODES_IGNORE);

double pisum = 0;
double myresult = dboard(numDartsMaster);
printf("parent result is %.9f\n", myresult);

int rc = MPI_Reduce(&myresult, &pisum, 1, MPI_DOUBLE, MPI_SUM, MPI_ROOT, everyone);

if (rc != MPI_SUCCESS)
printf("failure on mpi_reduce\n");

free(args[0]);
/*
* Parallel code here. The communicator "everyone" can be used
* to communicate with the spawned processes, which have ranks 0,..
* MPI_UNIVERSE_SIZE-1 in the remote group of the intercommunicator
* "everyone".
*/
//receive the results
int i=1;
MPI_Status status;

double avgpi = pisum;
avgpi += myresult; //include master's average in the result.
avgpi /= numWorkers;

printf("startTime = %d secs, %d microsecs\n", starttime.tv_sec);


// gettimeofday(&endtime, NULL);

// double totalTime = ((double)endtime.tv_sec + (double)endtime.tv_usec/1000000.0f) -
// ((double)starttime.tv_usec + (double)starttime.tv_usec/1000000.0f);

// printf("Total time: %.8f\n", totalTime);
printf("With %i workers, %i darts, estimated value of pi is: %.9f\n", numWorkers, numDarts, avgpi);

MPI_Finalize();


return 0;
}

我在第二次调用 gettimeofday 之前调用了 printf。如果第二次调用被注释掉,它只会打印出任何东西,否则它会崩溃。我在这个例子中注释掉了 gettimeofday,但这是导致 mpi 崩溃的调用。如果我取消注释它,它会再次开始崩溃并显示我提到的错误消息。

我想知道是否有人对为什么 gettimeofday 会这样做有任何见解。

最佳答案

在您的代码中,有一个或多个线程的产生

//the master counts as a worker, hence the -1
MPI_Comm_spawn("piworker", args, numWorkers-1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
&everyone, MPI_ERRCODES_IGNORE);

随后 args 被释放,如

free(args[0]);

这是预期的吗?释放此内存不会影响各个线程吗?

关于c - gettimeofday 会导致 MPI 崩溃吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14782507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com