Giter Site home page Giter Site logo

CUDA code won't run about coriander HOT 5 CLOSED

hughperkins avatar hughperkins commented on May 21, 2024
CUDA code won't run

from coriander.

Comments (5)

hughperkins avatar hughperkins commented on May 21, 2024

from coriander.

tanmayb123 avatar tanmayb123 commented on May 21, 2024

Got it. Thanks!

from coriander.

tanmayb123 avatar tanmayb123 commented on May 21, 2024

I tried this code:

#include <stdio.h>

#define SIZE 1000

__global__ void kernel_matrix_add(float *input1, float *input2, float *output) {
        const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
        output[idx] = input1[idx] + input2[idx];
}

__global__ void kernel_matrix_multiply(float *input1, float *input2, float *output) {
        const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
        output[idx] = input1[idx] * input2[idx];
}

__global__ void kernel_matrix_divide(float *input1, float *input2, float *output) {
        const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
        output[idx] = input1[idx] / input2[idx];
}

__global__ void kernel_matrix_subtract(float *input1, float *input2, float *output) {
        const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
        output[idx] = input1[idx] - input2[idx];
}


int main() {

        float * in1;
        float * in2;
        float * out;

        cuMemHostAlloc((void**)&in1, SIZE*sizeof(float));
        cuMemHostAlloc((void**)&in2, SIZE*sizeof(float));
        cuMemHostAlloc((void**)&out, SIZE*sizeof(float));

        for (int i = 0; i < SIZE; ++i) {
                in1[i] = i;
                in2[i] = i;
                out[i] = 0;
        }

        float * d_in1;
        float * d_in2;
        float * d_out;

        cudaMalloc(&d_in1, SIZE*sizeof(float));
        cudaMalloc(&d_in2, SIZE*sizeof(float));
        cudaMalloc(&d_out, SIZE*sizeof(float));

        cudaMemcpy(d_in1, in1, SIZE*sizeof(float), cudaMemcpyHostToDevice);
        cudaMemcpy(d_in2, in2, SIZE*sizeof(float), cudaMemcpyHostToDevice);
        cudaMemcpy(d_out, out, SIZE*sizeof(float), cudaMemcpyHostToDevice);

        kernel_matrix_multiply<<<SIZE / 1024 + 1, 1024>>>(d_in1, d_in2, d_out);

        cudaMemcpy(out, d_out, SIZE*sizeof(float), cudaMemcpyHostToDevice);

        printf("First 10 Results:\n");

        for (int i = 0; i < 10; ++i) {
                printf("%f\n", out[i]);
        }

}

But now I get this:

OpenCL platform: Apple
OpenCL device: GeForce GTX 1070
Segmentation fault: 11

When I try to run with nvcc:

cudatest.cu(32): error: identifier "cuMemHostAlloc" is undefined

1 error detected in the compilation of "/var/folders/5k/t9j2_ms918jgylxs810fmh8c0000gn/T//tmpxft_0000396d_00000000-9_cudatest.cpp1.ii".

Is there a way I can debug this?
Sorry! I'm just really new to this type of development!

from coriander.

tanmayb123 avatar tanmayb123 commented on May 21, 2024

I found it! This was the culprit: cudaMemcpy(out, d_out, SIZE*sizeof(float), cudaMemcpyHostToDevice);!
For some reason, this line worked on CUDA, but not cocl! It's supposed to be DeviceToHost not HostToDevice.

from coriander.

hughperkins avatar hughperkins commented on May 21, 2024

from coriander.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.