0

i want to write a kernel function that takes as input 2 CUSP matrices A and B,
then fills data into B in parallel.

#include <cusp/coo_matrix.h>
#include <cusp/print.h>
#include <iostream>

__global__ void kernel_example(cusp::coo_matrix<int,float,cusp::host_memory>* A,
cusp::coo_matrix<int,float,cusp::host_memory>* B){
    printf("hello from kernel...");
    //actual operations go here.
}

int main(void)
{
    // allocate storage
    cusp::coo_matrix<int,float,cusp::host_memory> A(4,3,6);
    cusp::coo_matrix<int,float,cusp::host_memory> B(4,3,6);

    // initialize matrix entries on host
    A.row_indices[0] = 0; A.column_indices[0] = 0; A.values[0] = 10;
    A.row_indices[1] = 0; A.column_indices[1] = 2; A.values[1] = 20;
    A.row_indices[2] = 2; A.column_indices[2] = 2; A.values[2] = 30;
    A.row_indices[3] = 3; A.column_indices[3] = 0; A.values[3] = 40;
    A.row_indices[4] = 3; A.column_indices[4] = 1; A.values[4] = 50;
    A.row_indices[5] = 3; A.column_indices[5] = 2; A.values[5] = 60;

    kernel_example<<<1,1>>>(A,B);
    cudaDeviceSynchronize();    

    return 0;
}

the following error ensues:

error: no suitable conversion function from "cusp::coo_matrix<int, float, cusp::host_memory>" to "cusp::coo_matrix<int, float, cusp::host_memory> *" exists

how do i go about it?

nza96
  • 41
  • 4
  • one possible method is outlined [here](https://stackoverflow.com/questions/22520669/how-to-get-raw-pointer-from-cusp-library-matrix-format). Your question is arguably a duplicate of that one. You should read the [cusp quick start guide](https://cusplibrary.github.io/md_quickstart.html), and understand the difference between `cusp::host_memory` and `cusp::device_memory` storage in cusp. To make any of this work, you need a matrix or array that is using `cusp::device_memory` storage. If you have something in host memory, it's easy to transfer it to device memory. – Robert Crovella Apr 24 '18 at 10:19

1 Answers1

-1

The error is because the function signature is for a pointer, and you're passing an object. You can pass by reference and it will build.

Should be

__global__ void kernel_example(cusp::coo_matrix<int, float, cusp::host_memory>& A,
    cusp::coo_matrix<int, float, cusp::host_memory>& B) {
    printf("hello from kernel...");
    //actual operations go here.
}
Mikhail
  • 7,749
  • 11
  • 62
  • 136
  • 1
    this is almost certainly not the right way. Yes, you can make the compile error go away with a pass-by-reference parameter, but you cannot actually make use of that in a CUDA kernel. – Robert Crovella Apr 24 '18 at 10:10
  • yes it is exactly the problem i found after using the code. you answered it in advance so thank you Robert! – nza96 Apr 24 '18 at 22:53
  • @RobertCrovella Sure, its not clear what this person is asking. Did they blindly post the sample code from the CUSP website? Is the real question about the difference between a device and host function? I'm voting to close. – Mikhail Apr 24 '18 at 23:08
  • Regardless of the question asked, I would never recommend that someone use reference parameters on a CUDA kernel. It's almost never correct. – Robert Crovella Apr 25 '18 at 00:17