Exception thrown in Cuda at cudaMemset2D after cudaMallocPitch

When compiling Cuda code in release mode, which contains cudaMallocPitch preceding cudaMemset2D,  I get the following exception:

"Cuda Error in /home/Yehonatans/CLionProjects/TestCUdaMallocPitch/main.cu @ 28. Error code: 1 Error string: invalid argument"

When the code is compiled in debug mode,  no exception occurs. In addition, when I compile the code from terminal, even in release mode, there is no exception.

Cmake code:

cmake_minimum_required(VERSION 3.24)
project(TestCUdaMallocPitch CUDA)

set(CMAKE_CUDA_STANDARD 17)

add_executable(TestCUdaMallocPitch main.cu)

set_target_properties(TestCUdaMallocPitch PROPERTIES
  CUDA_SEPARABLE_COMPILATION ON)

Cuda code

#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;
}

Cuda version 11.7

Clion version Build #CL-223.7571.171, built on November 28, 2022 (but the issue also happens at older versions)

0

Please sign in to leave a comment.