Skip to content
Permalink
Browse files
update
  • Loading branch information
carey committed Feb 2, 2022
1 parent 00cc678 commit 4969f4d4ccc9771f5b6723b64afae6a7d062af30
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 131 deletions.
BIN +0 Bytes (100%) .DS_Store
Binary file not shown.
BIN +0 Bytes (100%) code/.DS_Store
Binary file not shown.
BIN +0 Bytes (100%) code/distributed/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,94 @@
/**
* Author : Dr Carey Pridgeon
* Copyright : Dr Carey Pridgeon 2021
* Licence : Licensed under the Apache License, Version 2.0 (the "License");
* : you may not use this file except in compliance with the License.
* : You may obtain a copy of the License at
* : http://www.apache.org/licenses/LICENSE-2.0
* :
* : Unless required by applicable law or agreed to in writing,
* : software distributed under the License is distributed on
* : an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* : either express or implied. See the License for the specific
* : language governing permissions and limitations under the License.
*/

#include <iostream>
#include <string.h>
#include "mpi.h"
#include "omp.h"


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

MPI_Init(NULL, NULL);

// Find out rank, size
int world_rank, world_size, namelen;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

char node_name[MPI_MAX_PROCESSOR_NAME];

MPI_Get_processor_name(node_name, &namelen);
memset(node_name + namelen, 0, MPI_MAX_PROCESSOR_NAME - namelen);
const float floaty = 42;
int source = 0;
int destination = 5;

if (world_rank == source){
std::cout << "> hello from "<< node_name<< std::endl; //standard debugging code, remove later
MPI_Send(&floaty,1, MPI_FLOAT, destination, 0, MPI_COMM_WORLD);
}

if (world_rank == destination){
float local_floaty = 0.0; // just so we can be certain it's local to this node, and it's initialised with z, so we'll know if nothing's happening.
float second_var;
float third_var;

// this of course means you can't use z.
std::cout << "> hello from "<< node_name<< std::endl; //standard debugging code, remove later
MPI_Recv(&local_floaty, 1, MPI_FLOAT, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "> Float: " << local_floaty << " Received by "<< node_name<< std::endl;

/// OpenMP Code Begins that does almost nothing really
int i;
second_var = local_floaty;
#pragma omp parallel for
for (i=0;i<10;i++) {
local_floaty = local_floaty*10;
}
/// OpenMP Code Ends

// This produces the output (in various notations)
// 4.2 × 109 (scientific notation)

// 4.2e9 (scientific e notation)

// 4.2 × 109 (engineering notation)

// 4200000000 (real number)


std::cout << "> Transform finished, sending the result back to origin node: " << source << std::endl;

// now it's been changed we need to send it back to the source node, or rather, put it back into the communicator addressed to the head node ready to be collected
MPI_Send(&local_floaty,1, MPI_FLOAT, source, 0, MPI_COMM_WORLD);
}

// and now we repet the receive code, but this time for the source node

if (world_rank == source){

float local_floaty = 0.0; // same variable name, because this is a different scope, and will also be on a different machine
std::cout << "> hello from "<< node_name<< std::endl; //standard debugging code, remove later
MPI_Recv(&local_floaty, 1, MPI_FLOAT, destination, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "> Float: " << local_floaty << " Received back from the the node tasked with transform "<< std::endl;
}


MPI_Finalize();

return 0;
}
File renamed without changes.

This file was deleted.

0 comments on commit 4969f4d

Please sign in to comment.