Hi I am trying to write a code to output a random binary sequence.
This is what I have come up with but it keeps throwing an error when I try to compile it.
Can anyone help?
#include <StdAfx.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{int x = rand();
if (x/2 == 0);
printf('1');
else
printf('0');
}
Remove the ; after if (x/2 == 0)
#include <StdAfx.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int x = rand();
if (x/2 == 0)
printf('1');
else
printf('0');
}
Related
I get an error of 'portfolio' is not in scope for 'function.cpp'. I'm trying to call a function with a "vector of structs" passed in the parameters. But i'm assuming i'm not using porfolio correctly. How do get the size of the vector and change the elements inside of the vector? Thank you!
header.h
#include <iostream>
#include <vector>
struct Stocks {
std::string symbol;
std::string company;
int numberOfShares;
double priceOfShares;
double totalPrice;
int time;
};
void sellStock(std::vector<Stocks>& Portfolios, double*);
main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<Stocks>portfolio;
int main() {
sellStock(portfolio);
}
function.cpp
for(int i = 0; i < portfolio.size(); i++) {
porfolio[i].numberOfShares = portfolio[i].numberOfShares + 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/scan.h>
#include <thrust/execution_policy.h>
#include <iostream>
#include <thrust/transform.h>
struct text_functor {
text_functor() {}
__host__ __device__ int operator()(const char t) const {
if (t == '\n') return 0;
return 1;
}
};
void CountPosition1(const char *text, int *pos, int text_size)
{
thrust::transform(text, text + text_size, pos, text_functor());
}
int main() {
char s[4] = {'a', 'a', '\n', 'a'};
int r[4] = {0};
int *k;
cudaMalloc((void**) &k, sizeof(int) * 4);
CountPosition1(s, k, 4);
}
In thrust::transform, I mix host iterator s and device iterator k. This results in segmentation fault. If I change argument k to r in CountPosition1 the program will be correct.
Should all iterator in a thrust function be from same source(both host or both device)? Or is there something wrong in this code?
Yes, either all iterators should be from host containers, or all iterators should be from device containers.
Upon algorithm dispatch, thrust will dispatch either the host path or the device path. All iterators should be consistent with the dispatch method.
I can use inclusive_scan for an array on the cpu, but is it possible to do it with an array on the gpu? (commentated is the way that i know works but that I don't need). Alternatively, are there any other easy methods to perform an inclusive scan on an array in device memory?
Code:
#include <stdio.h>
#include <stdlib.h> /* for rand() */
#include <unistd.h> /* for getpid() */
#include <time.h> /* for time() */
#include <math.h>
#include <assert.h>
#include <iostream>
#include <ctime>
#include <thrust/scan.h>
#include <cuda.h>
#ifdef DOUBLE
#define REAL double
#define MAXT 256
#else
#define REAL float
#define MAXT 512
#endif
#ifndef MIN
#define MIN(x,y) ((x < y) ? x : y)
#endif
using namespace std;
bool errorAsk(const char *s="n/a")
{
cudaError_t err=cudaGetLastError();
if(err==cudaSuccess)
return false;
printf("CUDA error [%s]: %s\n",s,cudaGetErrorString(err));
return true;
};
double *fillArray(double *c_idata,int N,double constant) {
int n;
for (n = 0; n < N; n++) {
c_idata[n] = constant*floor(drand48()*10);
}
return c_idata;
}
int main(int argc,char *argv[])
{
int N,blocks,threads;
N = 100;
threads=MAXT;
blocks=N/threads+(N%threads==0?0:1);
double *c_data,*g_data;
c_data = new double[N];
c_data = fillArray(c_data,N,1);
cudaMalloc(&g_data,N*sizeof(double));
cudaMemcpy(g_data,c_data,N*sizeof(double),cudaMemcpyHostToDevice);
thrust::inclusive_scan(g_data, g_data + N, g_data); // in-place scan
cudaMemcpy(c_data,g_data,N*sizeof(double),cudaMemcpyDeviceToHost);
// thrust::inclusive_scan(c_data, c_data + N, c_data); // in-place scan
for(int i = 0; i < N; i++) {
cout<<c_data[i]<<endl;
}
}
If you read the thrust quick start guide you'll find one suggestion for handling "raw" device data: use a thrust::device_ptr:
You may wonder what happens when a "raw" pointer is used as an argument to a Thrust function. Like the STL, Thrust permits this usage and it will dispatch the host path of the algorithm. If the pointer in question is in fact a pointer to device memory then you'll need to wrap it with thrust::device_ptr before calling the function.
To fix your code, you would want to
#include <thrust/device_ptr.h>
and replace your existing call to thrust::inclusive_scan with the following 2 lines:
thrust::device_ptr<double> g_ptr = thrust::device_pointer_cast(g_data);
thrust::inclusive_scan(g_ptr, g_ptr + N, g_ptr); // in-place scan
Another approach would be to use thrust execution policies and modify your call like this:
thrust::inclusive_scan(thrust::device, g_data, g_data + N, g_data);
And there are various other possibilities as well.
I have following simple CUDA-Thrust code which adds 10 to device vector but the function is getting called on host side instead of device.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <stdio.h>
#include <thrust/device_vector.h>
__host__ __device__ int add(int x){
#if defined(__CUDA_ARCH__)
printf("In device\n");
#else
printf("In host\n");
#endif
return x+10;
}
int main(void)
{
thrust::host_vector<int> H(4);
H[0] = H[1] = H[2] = H[3] = 10;
thrust::device_vector<int> data=H;
std::transform(data.begin(), data.end(), data.begin(),add);
return 0;
}
What am I doing wrong here?
The thrust quick start guide has good examples to follow.
It looks like you have several issues, some already pointed out.
If you want to use thrust, you should use thrust::transform, not std::transform. std::transform has no knowledge of the GPU or CUDA or thrust, and will dispatch the host version of your add function. I'm not sure what that would do exactly when you pass a thrust::device_vector to it.
Thrust algorithms need to use function objects (functors) rather than bare CUDA __device__ functions, for the reason indicated by Jared (the thrust algorithm in your source code is actually host code. That host code cannot discover the address of a bare __device__ function). With this fix, you can be pretty certain that thrust will dispatch the device code path when working on device vectors.
Here's a modification of your code:
$ cat t856.cu
#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
struct my_func {
__host__ __device__
int operator()(int x){
#if defined(__CUDA_ARCH__)
printf("In device, x is %d\n", x);
#else
printf("In host, x is %d\n", x);
#endif
return x+10;
}
};
int main(void)
{
thrust::host_vector<int> H(4);
H[0] = H[1] = H[2] = H[3] = 10;
thrust::device_vector<int> data=H;
thrust::transform(data.begin(), data.end(), data.begin(),my_func());
return 0;
}
$ nvcc -o t856 t856.cu
$ ./t856
In device, x is 10
In device, x is 10
In device, x is 10
In device, x is 10
$
I've been having trouble calling my function in my code.
#include <iostream>
#include <cmath>
#include <cstdlib>
void bunky(int);
int main()
{
using namespace std;
bucky(20);
cin.get();
return 0;
}
void bucky(int x)
{
using namespace std;
cout << "My favorite number is " << x << endl;
}
When I try to compile this it says, "[Error]'bucky' was not declared in this scope" Any and all help would be great.
function declaration is wrong change
void bunky(int);
to
void bucky(int);