How do I use cudaMemcpy2D() DeviceToHost - visual-c++

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

Related

How to correctly calculate ACF in C++?

I would like to manually reproduce the method that authors of an article used in their research (DOI: 10.1038/s41598-017-02750-9 (Page 8. top)). It is mentioned as "ACF", so I wrote different functions:
1, a version based on a youtube video (https://youtu.be/ZjaBn93YPWo?t=417) using Alglibs Pearson correlation coefficient function
2, then another version based on the formula that is described in the article mentioned above
3, then another version based on the simplified formula described at an online ACF calculator page (https://planetcalc.com/7908/)
4, then a version based on the longer formula described there (https://planetcalc.com/7908/)
=> Yet, all of these give different output. However, method 3. is consistent with the output coming from the online calculator ran in my browser: https://planetcalc.com/7884/?d=.bTkjs.ymyQ8blXMoYiMgIOOmzhhI4fnckel.J5yEDWtV89Gz32Ch0kse2s
My code is here:
#include <iostream>
#define _WIN32_WINNT 0x0500
#include<windows.h>
//#include <cmath>
#include "alglib/alglibinternal.h"
#include "alglib/alglibmisc.h"
#include "alglib/ap.h"
#include "alglib/dataanalysis.h"
#include "alglib/diffequations.h"
#include "alglib/fasttransforms.h"
#include "alglib/integration.h"
#include "alglib/interpolation.h"
#include "alglib/linalg.h"
#include "alglib/optimization.h"
#include "alglib/solvers.h"
#include "alglib/specialfunctions.h"
#include "alglib/statistics.h"
#include "alglib/stdafx.h"
using namespace std;
double* normalize(double* _arr, int _s) {
double* output = new double[_s];
double mod = 0.0;
for (size_t i = 0; i < _s; ++i)
mod += _arr[i] * _arr[i];
double mag = sqrt(mod); //TODO: if 0, throw exc
double mag_inv = 1.0 / mag;
for (size_t i = 0; i < _s; ++i)
output[i] = _arr[i] * mag_inv;
return output;
}
void doACFyoutube(double* _ina, int _s)
// https://youtu.be/ZjaBn93YPWo?t=417 => the most unefficient, but understandable method
{
double* temp_x;
double* temp_y;
double* ACFoutput = new double[_s];
for(int shift = 0; shift < _s; shift++)
{
temp_x = new double[_s-shift];
temp_y = new double[_s-shift];
for(int cpy = 0; cpy < _s-shift; cpy++)
{
temp_x[cpy] = _ina[cpy];
temp_y[cpy] = _ina[cpy+shift];
}
temp_y = normalize(temp_y, _s-shift); //not sure if needed //TODO: leak
alglib::real_1d_array temp_x_alglib;
alglib::real_1d_array temp_y_alglib;
temp_x_alglib.setcontent(_s-shift, temp_x);
temp_y_alglib.setcontent(_s-shift, temp_y);
ACFoutput[shift] = alglib::pearsoncorr2(temp_x_alglib, temp_y_alglib); //Pearson product-moment correlation coefficient
delete temp_x;
delete temp_y;
}
for(int i=0; i<_s; i++)
cout << " lag = " << i << "\tACF(lag) = " << ACFoutput[i] << endl;
}
void doACFgoal(double* _ina, int _s)
// DOI: 10.1038/s41598-017-02750-9 => page 8, first equation (my goal is to reproduce this)
{
double mean = 0; //mean
for(int a = 0; a < _s; a++ )
mean += _ina[a];
mean /= _s;
double var = 0; //variance
for(int b = 0; b < _s; b++ )
var += (_ina[b]-mean)*(_ina[b]-mean);
var /= _s-1; //needed? (-1) a.k.a. Bessell's correction ?
double* ACFoutput = new double[_s];
for(int i = 0; i < _s; i++)
{
double temp_sum = 0;
for(int j = 1; j <= _s-i; j++)
temp_sum += (_ina[j]-mean)*(_ina[j+i]-mean);
ACFoutput[i] = (double)1/(((double)_s-(double)i)*var*var) * temp_sum;
}
for(int i=0; i<_s; i++)
cout << " lag = " << i << "\tACF(lag) = " << ACFoutput[i] << endl;
}
void doACFplanetcalcCoarse(double* _ina, int _s)
// https://planetcalc.com/7908/
{
double mean = 0; //mean
for(int a = 0; a < _s; a++ )
mean += _ina[a];
mean /= _s;
double* ACFoutput = new double[_s];
for(int i = 0; i < _s; i++)
{
double temp_sum1 = 0;
double temp_sum2 = 0;
for(int j = 0; j < _s-i; j++)
temp_sum1 += (_ina[j]-mean)*(_ina[j+i]-mean);
for(int k = 0; k < _s; k++)
temp_sum2 += (_ina[k]-mean)*(_ina[k]-mean);
ACFoutput[i] = temp_sum1 / temp_sum2;
}
for(int i=0; i<_s; i++)
cout << " lag = " << i << "\tACF(lag) = " << ACFoutput[i] << endl;
}
void doACFplanetcalcFine(double* _ina, int _s)
// https://planetcalc.com/7908/ => gives different output than the online calculator script, even though uses the longer formula described there
{
double* ACFoutput = new double[_s];
for(int k = 0; k < _s; k++)
{
double mean1 = 0; //mean of first N-k values
for(int a = 0; a < _s-k; a++ )
mean1 += _ina[a];
mean1 /= _s-k;
// cout << "\t mean of first N-" << k << " values = " << mean1 << endl;
double mean2 = 0; //mean of last N-k values
for(int a = k; a < _s; a++ )
mean2 += _ina[a];
mean2 /= _s-k;
// cout << "\t mean of last N-" << k << " values = " << mean2 << endl;
double temp_sum1 = 0;
double temp_sum2 = 0;
double temp_sum3 = 0;
for(int i = 0; i < _s-k; i++)
{
temp_sum1 += (_ina[i]-mean1)*(_ina[i+k]-mean2);
// cout << "\t\t temp_sum1 (" << i << ") = " << temp_sum1 << endl;
}
// cout << "\t temp_sum1 = " << temp_sum1 << endl;
for(int i = 0; i < _s-k; i++)
{
temp_sum2 += (_ina[i]-mean2)*(_ina[i]-mean2); //pow2
// cout << "\t\t temp_sum2 (" << i << ") = " << temp_sum2 << endl;
}
// cout << "\t temp_sum2 = " << temp_sum2 << endl;
for(int i = 0; i < _s-k; i++)
{
temp_sum3 += (_ina[i+k]-mean2)*(_ina[i+k]-mean2); //pow2
// cout << "\t\t temp_sum3 (" << i << ") = " << temp_sum3 << endl;
}
// cout << "\t temp_sum3 = " << temp_sum3 << endl;
ACFoutput[k] = temp_sum1 / (sqrt(temp_sum2)*sqrt(temp_sum3));
}
for(int i=0; i<_s; i++)
cout << " lag = " << i << "\tACF(lag) = " << ACFoutput[i] << endl;
}
int main()
{
//fullscreenhez
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
double ina[15] = {2,3,4,5,4,3,4,5,6,7,6,5,4,3,4}; //15 elem
for(int x=0; x<15; x++)
cout << ina[x] << ",";
cout << endl;
cout << endl;
// https://youtu.be/ZjaBn93YPWo?t=417 => the most unefficient, but understandable method
doACFyoutube(ina, 15); // ??? result doesn't match any other
cout << endl;
// DOI: 10.1038/s41598-017-02750-9 => page 8, first equation (my goal is to reproduce this)
doACFgoal(ina, 15); // ??? result doesn't match any other
cout << endl;
// https://planetcalc.com/7908/ (simplified formula)
doACFplanetcalcCoarse(ina, 15); //result equals to the online calculator result: https://planetcalc.com/7884/?_d=.bTkjs.ymyQ8blXMoYiMgIOOmzhhI4fnckel.J5yEDWtV89Gz32Ch0kse2s_
cout << endl;
// https://planetcalc.com/7908/ (longer formula)
doACFplanetcalcFine(ina, 15); // ??? result doesn't match any other
return 0;
}
The output looks like this:
As I do not have the original data they used in the publication, I can only rely on how consistent the output of my program is related to other codes output. But these outputs are different, and I do not know why. Could you please have a look at the code and help me end up in four equal outputs?
(Codeblocks project zipped here:
https://drive.google.com/file/d/1s3SeJSiDgk-hiMazp94HfFerL582VG2K/view?usp=sharing)

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;
}

Expression must have pointer-to-object type Error?

[Updated Code Pictutre][1]Can someone help me understand what I am doing wrong with my array in this code? It is giving me the same error for all my variables of the array
I dont know how to show you the whole code without adding like 5 pictures, i tried to capture all the important information
Ok I have posted the code here below
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
// output for a single input driving frequency
double m, c1, c2, k1, k2, r, Y;
m = c1 = c2 = k1 = k2 = r = Y = 0;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
double ceq, keq, wn, dampratio, staticamp;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
double cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double Fo, alpha, w, t;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
double Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
double phi = atan((ceq*w) / (keq - m*w*w));
double xp = Xp*sin(w*t - phi);
double H, Td, Ftwall, Ftbase;
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
//*****************************Section 1.2 **********************************************
// 1.2 output for a range of driving frequencies
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = keq = 0;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m); //natural frequency
dampratio = ceq / (2 * sqrt(m*keq)); // damping ratio
//Determine the Case
cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double staticamp[200], Xp[200], xp[200], Fo[200], phi[200], w[200];
double alpha[200], Td[200], H[200], Ftbase[200], Ftwall[200], r[200];
r[1] = .1;
for (int i = 0; i<200; i++) {
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r[i] * wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha[i] = atan(-c2*w / k2);
}
//Static amplitude
staticamp[i] = 0;
staticamp[i] = Fo / keq;
Xp[i] = (Fo / keq) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
phi[i] = atan((ceq*w) / (keq - m*w*w));
xp[i] = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td[i] = 0;
Td[i] = (Fo / (Y*keq)) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Frequanecy Response
H[i] = 0;
H[i] = 1 / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Force Transmissibilty to the bang
Ftbase[i] = 0;
Ftbase[i] = (Xp*sqrt(pow((k1 - keq*r[i] * r[i]), 2) + pow((c1*wn*r[i]), r[i]))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall[i] = 0;
Ftwall[i] = (Xp*sqrt((k1*k1) + pow((c1*r[i] * wn), 2))) / (Y*k2);
//increment r
r[i + 1] = r[i] + .1;
}
ofstream Data(" 1.2 Case 1 .txt"); // File Creation to import in text file to graph
for (int i = 0; i > 200; i++) { //NEED TO CHANGE FILE NAME FOR NEW DATA SET
Data << r[i] << "," << H[i] << "," << phi[i] << "," << Td[i] << "," << Ftbase[i] << "," << Ftwall[i] << endl;
}
//************************* Section 2 ****************
cout << "\t\tSection 2" << endl << endl;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
cout << "Which Case (1-5): ";
cin >> cas;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
phi = atan((ceq*w) / (keq - m*w*w));
xp = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
return 0;
}

running a C program on an MPI cluster

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;
}`

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;

Resources