When compiling Cuda code which contains cudaMallocPitch preceding cudaMemset2D.
Nvcc compilation line are :
- nvcc testMallocAndSetPitch.cu -O3 -o test
- nvcc testMallocAndSetPitch.cu -O2 -o test
- nvcc testMallocAndSetPitch.cu -O1 -o test
- nvcc testMallocAndSetPitch.cu -o test
When compile and execute first and second option exception occurs while when I preform the third and fourth options everything is ok.
What I am doing wrong?
#include <cuda.h>
#include <cuda_runtime.h>
#include <sstream>
#include <iostream>
inline void __checkCudaErrors(cudaError err, const char *file, const int line)
{
if (cudaSuccess != err) {
std::stringstream ss;
ss << "Cuda Error in " << file << " @ " << line << ". Error code: " << err << " Error string: " << cudaGetErrorString(err);
std::cerr << ss.str() << "\n";
throw ss.str();
}
}
#define checkCudaErrors(err) __checkCudaErrors(err, __FILE__, __LINE__);
int main()
{
uint32_t width = 500;
uint32_t height = 500;
uint32_t majorDimSize;
uint32_t *ptr;
checkCudaErrors(cudaMallocPitch((void**)&ptr, (size_t *)(&majorDimSize), sizeof(uint32_t) * width, height));
//checkCudaErrors(cudaMalloc((void**)&ptr, sizeof(uint32_t) * width*height));
std::cerr<<majorDimSize<<" "<<sizeof(uint32_t) * width<<" "<<width<<" " <<height<<std::endl;
checkCudaErrors(cudaMemset2D(ptr, majorDimSize, 0, sizeof(uint32_t) * width, height));
return 0;
}
How to reproduce
Compile the code and run by using CUda 11.7
Os Rocky Linux 8.6