running a C program on an MPI cluster - linux

I have a problem running an MPI program (written in C or C++) over a cluster comprising of two nodes.
Details:
OS: Ubuntu 16.04
No. of nodes: 2 (master and slave)
Everything works well. When I run a simple mpi_hello program on the cluster with 12 as an argument (no. of processes) I see 4 mpi-hello instances running on the slave node (checked using top).
Output on master node + mpi_hello instances running on the second node (slave node)
When I try to run another program (for instance a simple program calculating and printing prime numbers in a range) it is running on the master node but i don't see any instances of it on the slave node.
#include <stdio.h>
#include<time.h>
//#include</usr/include/c++/5/iostream>
#include<mpi.h>
int main(int argc, char **argv)
{
int N, i, j, isPrime;
clock_t begin = clock();
int myrank, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
printf("Hello from the processor %d of %d \n" , myrank, nprocs);
printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);
/* For every number between 2 to N, check
whether it is prime number or not */
printf("Prime numbers between %d to %d\n", 1, N);
for(i = 2; i <= N; i++){
isPrime = 0;
/* Check whether i is prime or not */
for(j = 2; j <= i/2; j++){
/* Check If any number between 2 to i/2 divides I
completely If yes the i cannot be prime number */
if(i % j == 0){
isPrime = 1;
break;
}
}
if(isPrime==0 && N!= 1)
printf("%d ",i);
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\nThe time spent by the program is %f\n" , time_spent);
while(1)
{}
MPI_Finalize();
return 0;
}
What could be the possible reasons behind it ?
Are there any other ways to check if it is running on the slave node as well ?
Thanks

Okay so here is a code I worked with. A vector containing first 500 integers. Now I want to divide them into 4 processes equally (i.e. each process gets 125 integers -- the first process gets 1-125, the second 126-250 and so on). I tried to use MPI_Scatter(). but I don't see the data equally divided or even divided. Do I have to use MPI_Recv() (I have another piece of code which is functional and uses only scatter to divide data equally).
Could you pint out any problems in the code. Thanks
int main(int argc, char* argv[])
{
int root = 0;
MPI_Init(&argc, &argv);
int myrank, nprocs;
MPI_Status status;
//variables for prime number calculation
int num1, num2, count, n;
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
char name[MPI_MAX_PROCESSOR_NAME + 1];
int namelen;
MPI_Get_processor_name(name, &namelen);
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
int size = 500;
int size1 = num2 / nprocs;
cout << "The size of each small vector is " << size1 << endl;
auto start = get_time::now(); //start measuring the time
vector<int> sendbuffer(size), recbuffer(size1); //vectors/buffers involved in the processing
cout << "The prime numbers between " << num1 << " and " << num2 << " are: " << endl;
if (myrank == root)
{
for (unsigned int i = 1; i <= num2; ++i) //array containing all the numbers from which you want to find prime numbers
{
sendbuffer[i] = i;
}
cout << "Processor " << myrank << " initial data";
for (int i = 1; i <= size; ++i)
{
cout << " " << sendbuffer[i];
}
cout << endl;
MPI_Scatter(&sendbuffer.front(), 125, MPI_INT, &recbuffer.front(), 125, MPI_INT, root, MPI_COMM_WORLD);
}
cout << "Process " << myrank << " now has data ";
for (int j = 1; j <= size1; ++j)
{
cout << " " << recbuffer[j];
}
cout << endl;
auto end = get_time::now();
auto diff = end - start;
cout << "Elapsed time is : " << chrono::duration_cast<ms>(diff).count() << " microseconds " << endl;
MPI_Finalize();
return 0;
}`

Related

Having trouble with pointers in C++

I am trying to access an array from inside of a function, but I get the
"Error C2065 'i': undeclared identifier." I know that I am making a mistake with the pointer. I was able to pull information from the array in the function below the one I'm having issues with, so I'm not sure why I am unable to do the same thing here. Thank you for your time.
#include <iostream>
#include <cmath>
using namespace std;
double mean(int size, int* numbers);
double sDeviation(int numOfScores, int average, int* scores);
int histogram(int numOfScores, int* scores); //<<<This is what I'm having trouble with
int main()
{
int count = 0;
int scores[100];
while (true)
{
int scoreToBeEntered;
cout << "Please enter a score: ";
cin >> scoreToBeEntered;
if(scoreToBeEntered == NULL)
cout << "No value entered" << endl;
else if(scoreToBeEntered != -1)
scores[count++] = scoreToBeEntered;
else
break;
}
for(int i = 9; i >= 0; i--)
cout << i << "|" << endl;
cout << "SD: " << sDeviation(count, mean(count, scores), scores) << endl;
system("pause");
return 0;
}
int histogram(int numOfScores, int* scores)//this is where the issue starts
{
int* bins = new int[10];
for(int i = 0; i < numOfScores; i++);
if(scores[i] >= 90) //<<<<This is the undeclared "i"
{
bins[9]++;
}
}
double sDeviation(int numOfScores, int average, int* scores)
{
double deviation = 0;
for (int i = 0; i < numOfScores; i++)
deviation += pow(scores[i] - average, 2);
return sqrt(deviation / numOfScores);
}
double mean(int size, int* numbers)
{
double sum = 0;
for (int i = 0; i < size; i++)
sum += numbers[i];
return sum / size;
}

Attempting to print a tree in C++ (Not using x's)

I am trying to print a tree using C++. I can print the tree using only the "/"'s, but I need to use both "/" and "\"'s on each side of the tree, with empty space in between, if that makes sense. I need to make the "cone part of the tree" with only 3 "for" loops.
I'm good with the base and the trunk, but I need help with the cone.
I know that I need to account for the empty spaces on each side of the cone and inside each side of the cones but everything I try messes it all up, and being as new as I am I'm having a hard time keeping it at 3 "for" loops. Also, my teacher looks down at using the internet as a learning resource, so anything outside of "for" loops for this program with throw red flags. Any help is appreciated.
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter a height for the cone of the tree. [3 - 15]: ";
int height;
cin >> height;
if(height < 3 || height > 15)
{
cout << "ERROR: Value entered is out of bounds." << endl;
system("pause");
exit(0);
}
int level = 0;
int space = 0;
int slashes = 0;
int base = 0;
int wood = 0;
int trunk = 0;
for (int level = 0; level < height; level++) //First "Cone" For Loop
{
for (int space = height - level - 1; space > 0; space--) //Second "cone" for loop
cout << ' ';
for (int slashes = 0; slashes < 2 * level + 1; slashes++) //Third "cone" for loop
cout << '/';
cout << endl;
}
for (int base = 0; base < 2 * height; base++)
cout << '-';
cout << endl;
for (int trunk = 0; trunk < (height / 2); trunk++)
{
for( int wood = 0; wood < height - 1; wood++)
cout << ' ';
cout << '|' << '|';
cout << endl;
}
system ("pause");
return 0;
}
Actual:
/
///
/////
///////
/////////
///////////
------------
||
||
||
Expected:
/\
/ \
/ \
/ \
/ \
/ \
------------
||
||
||
For anyone else Googling this, here is how I did it. It's ugly and probably wrong, but it works.
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter a height for the cone of the tree. [3 - 15]: ";
int height;
cin >> height;
if(height < 3 || height > 15)
{
cout << "ERROR: Value entered is out of bounds." << endl;
system("pause");
exit(0);
}
int level = 0;
int space = 0;
int base = 0;
int trunk = 0;
for (int level = 0; level < height; level++)
{
for (int space = height - level - 1; space > 0; space--)
cout << ' ';
cout << '/';
for (int space = 0; space < (2 * level); space++)
cout << ' ';
cout << '\\';
cout << endl;
}
for (int base = 0; base < 2 * height; base++)
cout << '-';
cout << endl;
for (int trunk = 0; trunk < (height / 2); trunk++)
{
for( int trunk = 0; trunk < height - 1; trunk++)
cout << ' ';
cout << '|' << '|';
cout << endl;
}
//system ("pause");
return 0;
}

need to input numbers and output as stars c++

this is my first question, i have to write a simple program that asks the user to input an integer, where according to the input, it outputs stars according to the input.
for example:
#include <iostream>
using namespace std;
int main()
{
int n=0;
char star='*';
cout<<"Enter number Desired "<<endl;
cin>> n;
star=n;
cout<<' \n'<<star<<endl;
cout<<' \n'<<star-1<<endl;
cout<<' \n'<<star-2<<endl;
cout<<' \n'<<star-3<<endl;
cout<<' \n'<<star-4<<endl;
system ("pause");
return 0;
}
You should use a for-loop for printing out stars one by one.
An example is given below:
for (int i = 0; i < n; i++) {
cout << "*" << endl;
}
To make this loop print out less and less stars in each row, use nested for-loops:
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
cout << "*" << endl;
}
cout << "\n" << endl;
}
This loop will print out n star characters in the first row, n-1 characters in the second row, and so on.
Let's say, if n == 5, then the output will be:
*****
****
***
**
*
This will print out a descending number of stars from the entered number:
#include <iostream>
using namespace std;
int main() {
int n=0;
char star='*';
cout<<"Enter number Desired "<<endl;
cin>> n;
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
cout << "*";
}
cout << " " << endl;
}
system ("pause");
return 0;
}

Prime number checker?

I'm trying to create a program that will check if a given number (between 1 and 1000) is prime but ran across a problem or two. The code I have below will run, but output 1000 times because of line 14 (for(int i = 3; i <= ELEMENTS; i++){) I know why it's running 1000 times but I can't find a way around it.
#include<iostream>
using namespace std;
int main(){
cout << "enter number of interest: ";
int num;
cin >> num;
const int ELEMENTS =1000;
bool multiples[ELEMENTS] = {};
for(int i = 3; i <= ELEMENTS; i++){
for(int j = 2; j <= i - 1; j++){
multiples[i]=true;
if(i % j == 0){
multiples[j]=false;
}
}
if((multiples[num] == true)){
cout << num << " is prime" << endl;
}
else
cout <<num<< " is not prime"<<endl;
}
return 0;
}
Move it out of for loop. Since variables it uses are defined out of for loop scope, it will work fine.
Edit: correct fragment:
for(int i = 3; i <= ELEMENTS; i++){
for(int j = 2; j <= i - 1; j++){
multiples[i]=true;
if(i % j == 0){
multiples[j]=false;
}
}
}
if (multiples[num] == true) {
cout << num << " is prime" << endl;
}
else
cout << num << " is not prime" << endl;

How do I use cudaMemcpy2D() DeviceToHost

I'm new to cuda and C++ and just can't seem to figure this out.
What I want to do is copy a 2d array A to the device then copy it back to an identical array B.
I would expect that the B array would have the same values as A, but there is something that I'm doing wrong.
CUDA - 4.2, compiling for win32, 64 bit machine, NVIDIA Quadro K5000
Here is the code.
void main(){
cout<<"Host main" << endl;
// Host code
const int width = 3;
const int height = 3;
float* devPtr;
float a[width][height];
//load and display input array
cout << "a array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
for (int j = 0 ; j < height; j ++)
{
a[i][j] = i + j;
cout << a[i][j] << " ";
}
cout << endl;
}
cout<< endl;
//Allocating Device memory for 2D array using pitch
size_t host_orig_pitch = width * sizeof(float); //host original array pitch in bytes
size_t pitch;// pitch for the device array
cudaMallocPitch(&devPtr, &pitch, width * sizeof(float), height);
cout << "host_orig_pitch: " << host_orig_pitch << endl;
cout << "sizeof(float): " << sizeof(float)<< endl;
cout << "width: " << width << endl;
cout << "height: " << height << endl;
cout << "pitch: " << pitch << endl;
cout << endl;
cudaMemcpy2D(devPtr, pitch, a, host_orig_pitch, width, height, cudaMemcpyHostToDevice);
float b[width][height];
//load b and display array
cout << "b array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
for (int j = 0 ; j < height; j ++)
{
b[i][j] = 0;
cout << b[i][j] << " ";
}
cout << endl;
}
cout<< endl;
//MyKernel<<<100, 512>>>(devPtr, pitch, width, height);
//cudaThreadSynchronize();
//cudaMemcpy2d(dst, dPitch,src ,sPitch, width, height, typeOfCopy )
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);
// should be filled in with the values of array a.
cout << "returned array" << endl;
for(int i = 0 ; i < width ; i++){
for (int j = 0 ; j < height ; j++){
cout<< b[i][j] << " " ;
}
cout<<endl;
}
cout<<endl;
system("pause");
}
Here is the output.
Host main A Array 0 1 2 1 2 3 2 3 4
host_orig_pitch: 12 sizeof(float): 4 width: 3 height: 3 pitch: 512
b array: 0 0 0 0 0 0 0 0 0
returned array 0 0 0
1.17549e-038 0 0 0 0 0
Press any key to continue . . .
If more information is need let me know and I'll post it.
Any help would be greatly appreciated.
As identified in comments, the original poster was supplying incorrect arguments to the cudaMemcpy2Dcall. The width argument for a transfer is always in bytes, so in the above code:
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);
should be
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width * sizeof(float), height, cudaMemcpyDeviceToHost);
Note this answer was added as a community wiki to get this question off the unanswered list

Resources