Reputation: 1305
I'm a bit of a CUDA newbie so was wondering if someone could help me out.
I read that pinning can seriously improve your programs performance and so am trying to do exactly that. I'm running my code on a GeForce GT 330 which has compute capability of 1.0.
When I run my program I get that cudaMallocHost failed to allocate memory and so I've condensed my problem down into a small example that can be seen below.
Mesh.hpp
#ifndef MESH_HPP_
#define MESH_HPP_
#include <cstddef>
#include <vector>
#include <driver_types.h>
class Mesh{
public:
Mesh();
~Mesh();
void pin_data();
std::vector<size_t> _a;
size_t* _a_pinned;
private:
void cuda_check(cudaError_t success);
};
#endif /* MESH_HPP_ */
Mesh.cpp
#include <iostream>
#include <cmath>
#include <vector>
#include <string.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include "Mesh.hpp"
Mesh::Mesh(){
for(size_t i = 0; i < 10; i++){
_a.push_back(i);
}
}
Mesh::~Mesh() {
cudaFreeHost(_a_pinned);
}
void Mesh::pin_data() {
size_t _a_bytes = sizeof(size_t) * _a.size();
cuda_check(cudaMallocHost((void **)_a_pinned, _a_bytes));
memcpy(_a_pinned, &_a[0], _a_bytes);
}
void Mesh::cuda_check(cudaError_t status) {
if (status != cudaSuccess) {
std::cout << "Error could not allocate memory result " << status << std::endl;
exit(1);
}
}
Main.cpp
#include <cstdlib>
#include <iostream>
#include "Mesh.hpp"
int main(int argc, char **argv){
Mesh *mesh = new Mesh();
mesh->pin_data();
delete mesh;
return EXIT_SUCCESS;
}
When I run my code the output is:
'Error could not allocate memory result 11'
Upvotes: 1
Views: 2865
Reputation: 151799
change this line:
cuda_check(cudaMallocHost((void **)_a_pinned, _a_bytes));
to this:
cuda_check(cudaMallocHost((void **)&_a_pinned, _a_bytes));
(only change is to add the ampersand)
cudaMalloc operations expect to modify a pointer value, therefore they must be passed the address of the pointer to modify, not the pointer itself.
That fixed it for me. I'm still a little puzzled by vectors of <size_t>
but to each his or her own.
If you want, as a suggestion, in your Mesh:cuda_check
method, you might add a line like so:
std::cout << "Error could not allocate memory result " << status << std::endl;
std::cout << "Error is: " << cudaGetErrorString(status) << std::endl; //add this line
Upvotes: 5