我使用 MPI 撰寫了一個多執行緒 C 程式來查找 2D 字符陣列中的回文。我啟動4個執行緒。現在所有執行緒都完成后,我想殺死執行緒并繼續串行作業嗎?我如何實作這一目標?代碼看起來像這樣:
int main(int argc, char *argv[]) {
// define some varibles
int foo;
// Kick off parallel work
MPI_Init (&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
// some work ...
}
if (rank == 1) {
// some work ...
}
if (rank == 2) {
// some work ...
}
if (rank == 3) {
// some work ...
}
MPI_Finalize();
// Here I want to print some results AFTER all threads are finished. E.g deallocate memory etc
printf("Found: %d", foo);
return 0;
}
uj5u.com熱心網友回復:
您對 MPI 的作業方式感到困惑。MPI 不使用執行緒,它使用獨立的行程。每個行程都執行整個主程式。* 因此,如果您只想讓一個行程作業,您可以if (i_am_working_process)在該代碼周圍設定一個條件。
*腳注:該標準實際上允許不同的模型,但在實踐中確實如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462102.html
上一篇:列印txt檔案的10個最佳結果
下一篇:無法從c中的文本檔案中讀取資料
