2013-02-22 1 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; 
    } 
Смежные вопросы