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

  • Home
  • SEARCH
  • 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 7168141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:43:10+00:00 2026-05-28T14:43:10+00:00

I need a device version of the following host code: double (**func)(double x); double

  • 0

I need a device version of the following
host code:

double (**func)(double x);

double func1(double x)
{
 return x+1.;
}

double func2(double x)
{
 return x+2.;
}

double func3(double x)
{
 return x+3.;
}

void test(void)
{
 double x;

 for(int i=0;i<3;++i){
  x=func[i](2.0);
  printf("%g\n",x);
 }

}

int main(void)
{
 func=(double (**)(double))malloc(10*sizeof(double (*)(double)));
 
 test();

 return 0;
}

where func1, func2, func3
have to be __device__ functions
and test
has to be a (suitably modified) __global__ kernel.

I have a NVIDIA GeForce GTS 450 (compute capability 2.1)


A working solution

#define REAL double

typedef REAL (*func)(REAL x);

__host__ __device__ REAL func1(REAL x)
{
    return x+1.0f;
}

__host__ __device__ REAL func2(REAL x)
{
    return x+2.0f;
}

__host__ __device__ REAL func3(REAL x)
{
    return x+3.0f;
}

__device__ func func_list_d[3];
func func_list_h[3];

__global__ void assign_kernel(void)
{
    func_list_d[0]=func1;
    func_list_d[1]=func2;
    func_list_d[2]=func3;
}

void assign(void)
{
    func_list_h[0]=func1;
    func_list_h[1]=func2;
    func_list_h[2]=func3;
}


__global__ void test_kernel(void)
{
    REAL x;
    for(int i=0;i<3;++i){
        x=func_list_d[i](2.0);
        printf("%g\n",x);
  }
}

void test(void)
{
    REAL x;
    printf("=============\n");
    for(int i=0;i<3;++i){
        x=func_list_h[i](2.0);
        printf("%g\n",x);
  }
}

int main(void)
{
    assign_kernel<<<1,1>>>();
    test_kernel<<<1,1>>>();
    cudaThreadSynchronize();

    assign();
    test();

    return 0;
}
  • 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-28T14:43:11+00:00Added an answer on May 28, 2026 at 2:43 pm

    function pointers are allowed on Fermi.
    This is how you could do it:

    typedef double (*func)(double x);
    
    __device__ double func1(double x)
    {
    return x+1.0f;
    }
    
    __device__ double func2(double x)
    {
    return x+2.0f;
    }
    
    __device__ double func3(double x)
    {
    return x+3.0f;
    }
    
    __device__ func pfunc1 = func1;
    __device__ func pfunc2 = func2;
    __device__ func pfunc3 = func3;
    
    __global__ void test_kernel(func* f, int n)
    {
      double x;
    
      for(int i=0;i<n;++i){
       x=f[i](2.0);
       printf("%g\n",x);
      }
    }
    
    int main(void)
    {
      int N = 5;
      func* h_f;
      func* d_f;
      h_f = (func*)malloc(N*sizeof(func));
      cudaMalloc((void**)&d_f,N*sizeof(func));
    
      cudaMemcpyFromSymbol( &h_f[0], pfunc1, sizeof(func));
      cudaMemcpyFromSymbol( &h_f[1], pfunc1, sizeof(func));
      cudaMemcpyFromSymbol( &h_f[2], pfunc2, sizeof(func));
      cudaMemcpyFromSymbol( &h_f[3], pfunc3, sizeof(func));
      cudaMemcpyFromSymbol( &h_f[4], pfunc3, sizeof(func));
    
      cudaMemcpy(d_f,h_f,N*sizeof(func),cudaMemcpyHostToDevice);
    
      test_kernel<<<1,1>>>(d_f,N);
    
      cudaFree(d_f);
      free(h_f);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Xcode Version 4.2 ,Build 4D199. I need to test an application for
I am wondering, do you need a specific device driver to read a usb
I need to build a device emulator which can communicate over real IP address
I need to capture keystokes from a wedge device and prevent any controls or
I need to communicate with a hardware device using TCP and with Windows I
I need to send data to a hardware device over serial port. I'm using
I need to communicate with a RS232 device, I have no specs or information
I need to draw a line one inch long on any device given a
I need to install a USB driver for a device so that it is
I need to change the the architectures of Any iPhone OS Device from Optimized

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.