Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4003666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:10:48+00:00 2026-05-20T08:10:48+00:00

I’m currently attempting to make a piece of CUDA code with a class that

  • 0

I’m currently attempting to make a piece of CUDA code with a class that will be used solely on the device side (i.e. host doesn’t need to know of it’s existence). However I cannot work out the correct qualifiers for the class (deviceclass below):

__device__ float devicefunction (float *x) {return x[0]+x[1];}

class deviceclass {
    private:
        float _a;

    public:
        deviceclass(float *x) {_a = devicefunction(x);}

        float getvalue () {return _a;}
};    

// Device code
__global__ void VecInit(float* A, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N) {
        deviceclass *test;

        test = new deviceclass(1.0, 2.0);

        A[i] = test->getvalue();
    }
}

// Standard CUDA guff below: Variables
float *h_A, *d_A;

// Host code
int main(int argc, char** argv)
{
    printf("Vector initialization...\n");
    int N = 10000;
    size_t size = N * sizeof(float);

    // Allocate
    h_A = (float*)malloc(size);
    cudaMalloc(&d_A, size);

    printf("Computing...\n");
    // Invoke kernel
    int threadsPerBlock = 256;
    int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
    VecInit<<<blocksPerGrid, threadsPerBlock>>>(d_A, N);

    // Copy result from device memory to host memory
    cudaMemcpy(h_A, d_A, size, cudaMemcpyDeviceToHost);

    //...etc
}

Setting Deviceclass as solely a __device__ throws an error as it’s called from a global function, however setting it as __device__ __host__ or __global__ seems unnecessary. Can someone point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:10:49+00:00Added an answer on May 20, 2026 at 8:10 am

    It turns out the qualifiers have to go on the member functions of the class, below is a fully working version:

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    void Cleanup(void);
    
    
    // Functions to be pointed to
    __device__ float Plus (float a, float b) {return a+b;}
    
    class deviceclass {
    
        private:
            float test;
    
        public:
            __device__ deviceclass(float a, float b) {
                test = Plus(a,b);
            }
    
            __device__ float getvalue() {return test;}
    };
    
    // Device code
    __global__ void VecInit(float* A, int N)
    {
        int i = blockDim.x * blockIdx.x + threadIdx.x;
        if (i < N) {
            deviceclass test(1.0, 2.0);
    
            A[i] = test.getvalue();
        }
    }
    
    // Standard CUDA guff below: Variables
    float *h_A, *d_A;
    
    // Host code
    int main(int argc, char** argv)
    {
        printf("Vector initialization...\n");
        int N = 10000;
        size_t size = N * sizeof(float);
    
        // Allocate
        h_A = (float*)malloc(size);
        cudaMalloc(&d_A, size);
    
        printf("Computing...\n");
        // Invoke kernel
        int threadsPerBlock = 256;
        int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
        VecInit<<<blocksPerGrid, threadsPerBlock>>>(d_A, N);
    
        // Copy result from device memory to host memory
        cudaMemcpy(h_A, d_A, size, cudaMemcpyDeviceToHost);
    
    
    
        // Verify result
        int i;
        for (i = 0; i < N; ++i) {
            cout << endl << h_A[i];
        }
    
        cout << endl;
    
        Cleanup();
    }
    
    void Cleanup(void)
    {
        // Free device memory
        if (d_A)
            cudaFree(d_A);
    
        // Free host memory
        if (h_A)
            free(h_A);
    
        cudaThreadExit();
    
        exit(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.