CUDA does not seem to compile - c++

I am currently running the CUDA 5.0 Toolkit on my Visual Studio 2012 Express.
I attempted to run the following code
I have searched high and low for methods of compiling .cu on Visual Studio but to no avail
Code I have attempted to compile:
//CUDA.cu
#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
using namespace std;
__global__ void Add(int* a, int* b)
{
a[0] += b[0];
}
int main()
{
int a = 5, b = 9;
int *d_a, *d_b;
cudaMalloc(&d_a, sizeof(int));
cudaMalloc(&d_b, sizeof(int));
cudaMemcpy(d_a, &a, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, &b, sizeof(int), cudaMemcpyHostToDevice);
Add<<< 1 , 1 >>>(d_a, d_b);
cudaMemcpy(&a, d_a, sizeof(int) , cudaMemcpyDeviceToHost);
cout << a << endl;
return 0;
}
The compiler shows an error in the line
Add<<< 1 , 1 >>>(d_a, d_b);
Where it says "Error:expected an expression"
Any attempts to compile this code results in a success. but no .exe is to be found hence I cannot debug whatsoever.
Unable to start program 'C:\Users\...\CUDATest3.exe'
The system cannot find the file specified
Any help whatsoever is VERY MUCH appreciated. Thanks
CK

Although I would love to understand as to WHY in my computer CUDA decided that VS is a deplorable program to be married to, I have taken a shortcut by handcuffing both of said program by means of copying from the templates provided by the CUDA installation toolkit. (The CUDA samples.)
Apparently those samples have everything already set up and ready to go; all you need to do is edit the code from inside the solution itself. For some reason the code would not work if I had written all of the code from square one.
I still have no idea as to why I am unable to get it to run from scratch but I guess
editing the sample templates would give a much satisfactory result
if one does not have the time for it.

Related

Error with Lambda Expression C++ in VS Code

I'm trying to sort an array m of class measure using a lambda expression as follows:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
struct measure{
int day;
int cow;
int change;
};
int main()
{
int N;
cin >> N;
measure m[N];
for (int i = 0; i < N; i++){
measure m_i;
cin >> m_i.day >> m_i.cow >> m_i.change;
m[i] = m_i;
}
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
}
However, an error occurs when trying to build the task in VS Code (using C++17):
error: expected expression
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
^
1 error generated.
Build finished with error(s).
I've tested this code on other compilers with no difficulties. Why is this error happening on VS Code?
Okay, so it does not look like you are compiling with c++17. I can reproduce this error if I roll back the gcc version to 4.x and leave the standard up to the compiler. Here is how it would look on an older compiler. Here is how it would run properly on a newer compiler. Most likely that you are using something like c++98 or c++03.
Note - I have taken the liberty of modifying your code to make it more C++. Also, please stop with using namespace std.
A simple fix is to just make sure that you are using the right version of the compiler basis the features you need. Lambdas in C++ were introduced in C++11, so clearly you are using a compiler that is at a much lower version.
VS Code does not have a compiler built-in. It uses the system compiler you have installed. You can configure things to use the system compiler and pass the flag --std=c++17 to the compiler command line.
Both gcc and clang++ support this command line flag.

Unable to load armadillo Cube<uword> when using RcppArmadillo

I was prepossessing data in C++ using the Armadillo library. The program end product is a ucube, which is a cube filled with unsigned integers. After its run, I want to load the ucube to R to perform some final statistical tests. To do so, I made a C++ function that load the ucube returning an array.
But it does not work!
I got the following warning: "warning: Cube::load(): incorrect header in B.bin" and the program returns a 0x0x0 array.
Trying to find why, I made a toy C++ program, which works fine. It is able to load the cubes without any problem.
#include <iostream>
#include <armadillo>
using namespace arma;
void read_cubes(char const* A, char const* B){
cube C;
ucube D;
C.load(A, arma_binary);
D.load(B, arma_binary);
}
int main(int argc, char** argv){
cube A = randu<cube>(5,5,5);
ucube B = randi<ucube>(5,5,5, distr_param(1, 10));
A.save(argv[1], arma_binary);
B.save(argv[2], arma_binary);
read_cubes(argv[1], argv[2]);
}
But I do not know why, doing the same steps in R does not work. To illustrate, please run the toy program as ./a.out A.bin B.bin. It will yield the Cube<double> A.bin and the Cube<uword> B.bin, which I will mention later.
The problem
If I source the following C++ code with Rcpp::sourceCpp and I try to read the Cube<double> A.bin with read_cube("A.bin") it works, but if I do the same for the Cube<uword> B.bin with read_ucube("B.bin") it does not (I get the warning).
#include <RcppArmadillo.h>
#include <iostream>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::cube read_cube(char const* x){
arma::cube A;
A.load(x, arma::arma_binary);
return A;
}
// [[Rcpp::export]]
arma::ucube read_ucube(char const* x){
arma::ucube B;
B.load(x, arma::arma_binary);
return B;
}
Of course I could cast the Cube<uword> to a Cube<double> before ending the C++ program, but I would like to know why this happen and if it is possible to load a Cube<uword> in RcppArmadillo. Because it should be possible, right?
Unfortunately R still only supports 32 bit integers, so RcppArmadillo forces Armadillo to use 32 bit integers. This is done by defining ARMA_32BIT_WORD before including the armadillo header. See RcppArmadillo's configuration here.
You can apply the same "trick" with your Armadillo programs like so:
#define ARMA_32BIT_WORD
#include <armadillo>
One of the effects is that ucube (Cube<uword>) will use 32 bit unsigned integers.
After doing the above trick, recompile your Armadillo programs and save the ucubes again. They can then be loaded in RcppArmadillo.

Loading a sparse matrix Armadillo C++

I'm having a bit of a strange problem. I generally use the CLion IDE to compile and run code but I want to make use of a profiler to try to optimize my software and CLion does not include a profiler extension. So I need to compile and run at command line.
OK, no problem but for some reason when I do so, I get errors when loading sparse matrices saved in the form arma_binary (which the Armadillo documentation states is the only possible file format for sparse matrices).
The following is an example script saved as main.cpp:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
void DenseMult (mat& I, mat& B);
void SparseMult (sp_mat& I, mat& B);
int main() {
mat Int; Int.load("intMatDense.mat");
cout << Int.n_cols << endl;
sp_mat IntSp; IntSp.load("intMatSparse.mat", arma_binary);
cout << IntSp.n_cols << endl;
mat B; B.load("biomassMat.mat");
DenseMult(Int, B);
SparseMult(IntSp, B);
return 0;
}
void DenseMult (mat& I, mat& B) {
I * B;
}
void SparseMult (sp_mat& I, mat& B) {
I * B;
}
Which gives the following output when built and run in CLion:
/home/jack/CLionProjects/Optimization/cmake-build-debug/Optimization
307
307
Process finished with exit code 0
So far so good but when I do (what I think is) the same at the command line:
jack#jack-MacBookPro:~/CLionProjects/Optimization$ g++ main.cpp -o main -O2 -larmadillo
jack#jack-MacBookPro:~/CLionProjects/Optimization$ ./main
I get the following output:
307
warning: SpMat::load(): inconsistent data in intMatSparse.mat
0
error: matrix multiplication: incompatible matrix dimensions: 0x0 and 307x3
terminate called after throwing an instance of 'std::logic_error'
what(): matrix multiplication: incompatible matrix dimensions: 0x0 and 307x3
Aborted (core dumped)
Is it possible I need to link to the Armadillo library differently in order to access the sparse matrix functionality?
Thanks in advance.
Jack

CUDA - Simple adder program always gives zero

I have a problem with a simple CUDA program which just adds two numbers. I run it on a laptop with a Geforce GT320M GPU on Windows 7. I compile this program with Visual Studio 2013 (I don't know if it means something). The problem is that I always get 0 as a result. I tried to check the given parameters (just return with all the parameters given to the method in an array) and they all seemed to be 0. I run this program in an other computer (at university) and there it runs completely fine, and returns the correct result. So I think there should be some setting problem, but I am not sure of it.
#include <cuda.h>
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void add(int a, int b, int* c)
{
*c = a + b;
return;
}
int main(int argc, char** argv)
{
int c;
int* dev_c;
cudaMalloc((void**)&dev_c, sizeof(int));
add << <1, 1 >> >(1, 2, dev_c);
cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);
printf("a + b = %d\n", c);
cudaFree(dev_c);
return 0;
}
I also run this code snippet that I found somewhere.
cudaSetDevice(0);
cudaDeviceSynchronize();
cudaThreadSynchronize();
This isn't returning anything.
If you are using the typical CUDA template to create a new Visual Studio project using CUDA, then you have to take care of correctly setting the compute capability for which to compile, changing the default values if needed. This can be done by setting, for example,
compute_12,sm_12
in the CUDA C/C++ Configuration Properties. In your case, the default compute capability was 2.0, while your card is of a previous architecture. This was the source of your mis-computations.
P.S. As of September 2014, CUDA 6.5 is the only version of CUDA supporting Visual Studio 2013, see Is Cuda 6 supported with Visual Studio 2013?.

C++ array Visual Studio 2010 vs Bloodshed Dev-C++ 4.9.9.2

This code compiles fine in Bloodshed Dev-C++ 4.9.9.2, but in Visual Studio 2010 I get an error: expression must have a constant value. How do I make an array after the user input about array size without using pointers?
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int size = 1;
cout << "Input array size ";
cin >> size;
int array1[size];
system("PAUSE");
return 0;
}
Use an std::vector instead of an array (usually a good idea anyway):
std::vector<int> array1(size);
In case you care, the difference you're seeing isn't from Dev-C++ itself, it's from gcc/g++. What you're using is a non-standard extension to C++ that g++ happens to implement, but VC++ doesn't.
The ability to size automatic arrays using a variable is part of C, not part of C++, and is an extension that GCC seems to want to foist on us all. And DevC++ is an unholy piece of cr*p, although it is not at fault here. for a change (this is entirely GCC's doing) - I can't imagine why you (or anyone else) would want to use it.
You should really compile your C++ code with GCC with flags that warn you about stuff like this. I suggest -Wall and -pedantic as a minimum.
Or
int array1 = new int[size];
will work aswell I believe (been a month or 3 since I last touched C++)
But indeed, if using C++, go for an std::vector, much more flexible.