0

I want to use cudaMemset for non-integer types to tidy the code as below, where I used the glm::vec2 type just as an example. However, cudaMemset appears to work only for integers.

So, are there any possible equivalents?

#include <glm/glm.hpp>
#include <cuda.h>

int main(){
    glm::vec2* ptr1;
    int num = 10;

    cudaMallocManaged(&ptr1, num*sizeof(glm::vec2));

    cudaMemset(ptr1, {0, 0}, num*sizeof(glm::vec2)); // wish to do sth like this, but it doesn't work
    // for (size_t i = 0; i < num; i++) ptr1[i] = {1, 0}; // want to replace this

    return 0;
}
Kaz
  • 105
  • 9
  • What about `cudaMemset2D` ? – Richard Critten Jun 23 '21 at 11:51
  • 1
    cudaMemset doesn't work for integer types. Bytes sized values only. – talonmies Jun 23 '21 at 12:41
  • 4
    in addition to the suggestions in the linked duplicate, specifically since you are doing `cudaMallocManaged` here, it should not be necessary to use the CUDA runtime API to initialize the data. It should be possible to use host methods such as `std::fill` – Robert Crovella Jun 23 '21 at 15:39
  • float data types are defined in a way that, if all individual bytes are 0, the value is 0.0. For other values: It is quite easy to create a very fast kernel, initializing the memory in parallel. – Sebastian Jun 24 '21 at 05:13
  • @RobertCrovella `std::fill` apparently doesn't work for all types, does it? I got a compile error "error: no instance of overloaded function "std::fill" matches the argument list argument types are: (glm::vec2 *, glm::vec2 *, {...})" – Kaz Jun 24 '21 at 10:51
  • I don't have any trouble using `std::fill` with a pointer to `glm::vec2`. See [here](https://pastebin.com/FSQJ5g14). Maybe it doesn't like your initializer. And yes, there probably are some types that you can't use `std::fill` with. However `glm::vec2` doesn't appear to be one of them. – Robert Crovella Jun 24 '21 at 13:40
  • @RobertCrovella You were right. My initialiser was incorrect. Thanks for lettling me know! – Kaz Jun 24 '21 at 14:53

0 Answers0