Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include "mpi.h"
- using namespace std;
- bool isPrimeNum(int num) {
- for (int i = 2; i < num; i++) {
- if (num % i == 0) {
- return true;
- }
- }
- return false;
- }
- int main(int argc, char* argv[]) {
- MPI_Init(&argc, &argv);
- // Get world rank
- int rankWorld;
- MPI_Comm_rank(MPI_COMM_WORLD, &rankWorld);
- //
- int ranksWorldCount;
- MPI_Comm_size(MPI_COMM_WORLD, &ranksWorldCount);
- // Get the group from the default communicator
- MPI_Group groupWorld;
- MPI_Comm_group(MPI_COMM_WORLD, &groupWorld);
- //
- vector<int> groupPrimeNumProcesses, groupOtherProcesses;
- for (int i = 0; i < ranksWorldCount; i++) {
- if (!isPrimeNum(i)) {
- groupPrimeNumProcesses.push_back(i);
- }
- else {
- groupOtherProcesses.push_back(i);
- }
- }
- // *** CART ***
- int ranksPrimeNumCount = groupPrimeNumProcesses.size();
- MPI_Group groupPrimeNum;
- MPI_Group_incl(groupWorld, ranksPrimeNumCount, &groupPrimeNumProcesses[0], &groupPrimeNum); // MPI_COMM_NULL
- MPI_Comm communicatorPrimeNum;
- MPI_Comm_create(MPI_COMM_WORLD, groupPrimeNum, &communicatorPrimeNum);
- if (communicatorPrimeNum != MPI_COMM_NULL)
- {
- // Get PrimeNum communicator rank
- int rankPrimeNum;
- MPI_Comm_rank(communicatorPrimeNum, &rankPrimeNum);
- const int ndims = 1;
- int dim_size[ndims] = { ranksPrimeNumCount };
- int periods[ndims] = { 1 };
- MPI_Comm communicatorCart;
- MPI_Cart_create(communicatorPrimeNum, ndims, dim_size, periods, 0, &communicatorCart);
- int rank_from, rank_to;
- MPI_Cart_shift(communicatorCart, 0, 1, &rank_from, &rank_to);
- double A = rand() % 500;
- double B;
- MPI_Status status;
- MPI_Sendrecv(&A, 1, MPI_DOUBLE, rank_to, 123, &B, 1, MPI_DOUBLE, rank_from, 123, communicatorCart, &status);
- printf("[Cart] Rank global = %i, from Rank %i to Rank %i\n", rankWorld, rank_from, rank_to);
- MPI_Comm_free(&communicatorCart);
- MPI_Comm_free(&communicatorPrimeNum);
- }
- // MPI_Comm_free(&communicatorPrimeNum);
- MPI_Group_free(&groupPrimeNum);
- // *** CART END ***
- // *** GRAPH ***
- int ranksOtherCount = groupOtherProcesses.size();
- MPI_Group groupOther;
- MPI_Group_incl(groupWorld, ranksOtherCount, &groupOtherProcesses[0], &groupOther); // MPI_COMM_NULL
- MPI_Comm communicatorOther;
- MPI_Comm_create(MPI_COMM_WORLD, groupOther, &communicatorOther);
- if (communicatorOther != MPI_COMM_NULL)
- {
- int* index = new int[ranksOtherCount];
- for (int k = 0; k < ranksOtherCount; k++) {
- index[k] = ranksOtherCount - 1 + k;
- }
- int* edges = new int[2 * ranksOtherCount - 2];
- for (int k = 0; k < ranksOtherCount - 1; k++) {
- edges[k] = k + 1;
- edges[ranksOtherCount - 1 + k] = 0;
- }
- MPI_Comm Gr_Comm;
- MPI_Graph_create(communicatorOther, ranksOtherCount, index, edges, false, &Gr_Comm);
- int Rank;
- MPI_Comm_rank(Gr_Comm, &Rank);
- int Num;
- MPI_Graph_neighbors_count(Gr_Comm, Rank, &Num);
- int* Neighbors = new int[Num];
- MPI_Graph_neighbors(Gr_Comm, Rank, Num, Neighbors);
- for (int k = 0; k < Num; k++) {
- int rankIn;
- MPI_Status St;
- MPI_Sendrecv(&Rank, 1, MPI_INT, Neighbors[k], 5, &rankIn, 1, MPI_INT, Neighbors[k], 5, Gr_Comm, &St);
- printf("[Graph] Rank global = %i, from Rank %i to Rank %i\n", rankWorld, Rank, Neighbors[k]);
- }
- MPI_Comm_free(&Gr_Comm);
- MPI_Comm_free(&communicatorOther);
- delete[] Neighbors;
- delete[] edges;
- delete[] index;
- }
- MPI_Group_free(&groupOther);
- // *** GRAPH END ***
- MPI_Group_free(&groupWorld);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement