我是CUDA新手,所以想知道有没有人能帮我。
我读到固定可以大大提高你的程序的性能,所以我正试图做到这一点。我在GeForce GT 330上运行我的代码,它的计算能力是1.0。
当我运行我的程序时,我得到cudaMallocHost无法分配内存,因此我将我的问题压缩到一个小示例中,如下所示。
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;
}当我运行我的代码时,输出是:
‘错误无法分配内存结果11’
发布于 2013-03-03 08:38:54
更改此行:
cuda_check(cudaMallocHost((void **)_a_pinned, _a_bytes));要这样做:
cuda_check(cudaMallocHost((void **)&_a_pinned, _a_bytes));(唯一的变化是添加了“与”符号)
cudaMalloc操作期望修改指针值,因此它们是must be passed the address of the pointer to modify,而不是指针本身。
帮我修好了。我仍然对<size_t>的向量感到有点困惑,但每个人都有自己的想法。
如果需要,作为建议,可以在Mesh:cuda_check方法中添加一行,如下所示:
std::cout << "Error could not allocate memory result " << status << std::endl;
std::cout << "Error is: " << cudaGetErrorString(status) << std::endl; //add this linehttps://stackoverflow.com/questions/15180832
复制相似问题