Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Coded Computation
- //
- // Created by Pardis Malekzadeh on 6/23/17.
- // Copyright © 2017 Pardis Malekzadeh. All rights reserved.
- //
- #include <iostream>
- #include <mpi.h>
- #include <stdlib.h>
- using namespace std;
- const int n = 2;
- const int p = 8;
- const int ntasks = 4;
- const int nsects = p/ntasks;
- int req_results = 2;
- double F[p][n];
- void slave(int myrank){
- int x[n];
- cout << "Im a slave, my rank is"<<myrank<<"this is part 1"<<endl;
- /*MPI::COMM_WORLD.Recv(x, n, MPI::DOUBLE,0, myrank);*/
- MPI::COMM_WORLD.Bcast(x, n,
- MPI::DOUBLE, 0);
- cout << "Im a slave, my rank is"<<myrank<<"this is part 2"<<endl;
- /////////////////
- double result[nsects];
- for (int i=0;i<nsects;i++){
- result[i]=0.0;
- }
- for (int i=0;i<nsects;i++){
- for (int j=0;j<n;j++){
- int StartIndex = (myrank-1)*nsects;
- result[i]+= F[StartIndex+i][j]*x[j];
- }
- }
- /////////////////
- MPI::COMM_WORLD.Send(result, nsects, MPI::DOUBLE, 0, myrank+ntasks);
- cout << "Im a slave, my rank is"<<myrank<<"this is part 3"<<endl;
- }
- void master(){
- double x[n];
- for (int i = 0; i < n; ++i)
- {
- x[i] = i;
- }
- /*for (int i = 0; i < n; ++i)
- {
- cin >> x[i];
- }*/
- cout << "Im master,this is part 1"<<endl;
- /*for (int rank = 1; rank <= ntasks; ++rank) {
- cout << "rank " << rank << endl;
- MPI::COMM_WORLD.Send(x, n, MPI::DOUBLE, rank, rank);
- }*/
- MPI::COMM_WORLD.Bcast(x, n,
- MPI::DOUBLE, 0);
- cout << "Im master,this is part 2"<<endl;
- double result[100];
- for (int i = 0; i < p; ++i) {
- result[i] = 0;
- }
- for (int i = 0; i < p; ++i) {
- cout<<result[i];
- }
- MPI::Request requests[ntasks];
- for (int rank = 1; rank <= ntasks; ++rank) {
- cout << (rank-1)*nsects << endl;
- requests[rank-1] = MPI::COMM_WORLD.Irecv(&result[(rank-1)*nsects], nsects, MPI::DOUBLE, rank, rank+ntasks);
- }
- cout << "Im master,this is part 3"<<endl;
- int recv_results = 0;
- do {
- for (int rank = 1; rank <= ntasks; ++rank) {
- if (requests[rank-1].Test())
- recv_results++;
- }
- } while (recv_results < req_results);
- cout << "Im master,this is part 4"<<endl;
- }
- int main(int argc, char* argv[]) {
- cout<< "Hello!" << '\n';
- for (int i = 0; i < p; ++i){
- for (int j = 0; j < n; ++j) {
- F[i][j] = rand() % 100 + 1;
- }
- }
- MPI::Init(argc, argv);
- int myrank = MPI::COMM_WORLD.Get_rank();
- int np = MPI::COMM_WORLD.Get_size();
- if (myrank == 0) {
- cout << "Running on "<< np << " Processes "<< '\n';
- master();
- } else {
- cout << "Greetings from process " << myrank << '\n';
- slave(myrank);
- }
- MPI::Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement