2013-02-22 26 views
0

我有一個巨大的sequntial程序,其中我想與MPI和CUDA並行化一些算法。如何正確分離順序部分和並行? 問題在於並行算法的嵌套,以及使用slurm或loadLeveler以及(例如,在我的MPI集羣上,我不能寫如:mpirun -n 1 a.out: -n 2 b.out)。在順序程序中分離並行MPI部分的最佳方法

例子:

int main() 
{ 
    funcA(); 
} 
void funcA() 
{ 
    funcB(); 
} 

void funcB() 
{ 
    parallel algo starts here.... 
} 

回答

1

我已經找到了一個很好的解決方案,對於這個問題。這是示例代碼:

#include <iostream> 
#include <mpi.h> 
#include <unistd.h> 

using namespace std; 

int main(int argc, char** argv) { 

MPI_Init(&argc, &argv); 
int r; 
MPI_Comm_rank(MPI_COMM_WORLD, &r); 

if (r == 0) { 
    cout << "[GUI]Start perfoming initialization...." << endl; 
    sleep(2); 
    cout << "[GUI]Send command to start execution...." << endl; 
    int command = 1; 
    //TODO: now it's hardcoded to send data to 1 proc 
    MPI_Send(&command, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); 
    cout << "[GUI]Waiting for execution results..." << endl; 
    int buf[5]; 
    MPI_Recv(&buf, 5, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
    for (int i=0; i<5; i++) 
    { 
     cout << "buf["<< i << "] = " << buf[i] << endl; 
    } 
} else { 
    int command; 
    MPI_Recv(&command, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
    cout << "Received command: " << command << endl; 
    if (command == 1) { 
     cout << "[ALGO]Receive command to start execution" << endl; 
     sleep(2); 
     cout << "[ALGO]Send computed data..." << endl; 
     int buf[5] = {5,4,3,2,1}; 
     MPI_Send(&buf, 5, MPI_INT, 0, 0, MPI_COMM_WORLD); 
    } 
} 


MPI_Finalize(); 
return 0; 
    } 
相關問題