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 4614048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:38:30+00:00 2026-05-22T01:38:30+00:00

Hey there, I want to evaluate a mathematical function (user-defined) which returns several values

  • 0

Hey there,
I want to evaluate a mathematical function (user-defined) which returns several values in an array (this function is a vector based function f:R^n->R^m with n input coordinates and m output functions) in C++ for certain parameters, e.g.:

double *my_func(const mxArray *point)
{
    double *dat = mxGetPr(point);
    double *vals = new double[ 3 ];

    vals[0] = dat[0]*dat[0]*dat[0]*dat[0]*dat[0];
    vals[1] = sin(dat[0])*dat[1]*dat[2]*dat[2]*cos(dat[1]);
    vals[2] = exp(dat[0])*sin(dat[0])*dat[3];

    double *pnt = vals; 
    return pnt;
}

Currently I do this on the CPU. So I call the function once and get back an array with all function values. As I want to parallelize it now on the GPU, I thought about how to do it.

I assume it would be kind of stupid to evaluate my_func() completely in each thread since than each thread would calculate the whole function-array. Is this the right assumption?

Would there be any way to comfortable calculate only the n-th element of the function-array and return it, so that 5 threads could easily calculate the function-array in parallel instead of one CPU calculating it completely ‘alone’?

The only way I could think off was:

double my_func0(const mxArray *point)
{
    double *dat = mxGetPr(point);
    return dat[0]*dat[0]*dat[0]*dat[0]*dat[0];
}
double my_func1(const mxArray *point)
{
    double *dat = mxGetPr(point);
    return sin(dat[0])*dat[1]*dat[2]*dat[2]*cos(dat[1]);
}
double my_func2(const mxArray *point)
{
    double *dat = mxGetPr(point);
    return exp(dat[0])*sin(dat[0])*dat[3];
}

etc… But this would be quite ‘uncomfortable’ for the user who uses the program later because he always would have to create new C++ functions if he wants to extend the function-array instead of just adapting ONE single C++-function. And a further problem would be: I have to dynamically call the function since the number of functions is ‘dynamic’ and thus I would have to do a call to my_func_%%i%% and don’t know if this is a good way to do it… So the question is if there would be a better way to deal with this problem?

  • 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-22T01:38:31+00:00Added an answer on May 22, 2026 at 1:38 am

    When you say “user_defined” I presume you mean that someone else writes my_func() and then your code calls it?

    If this is the case, consider running many calls to my_func() in parallel rather than trying to break the function up. This means whoever writes my_func() only needs to write one function, and you will be responsible for delegating multiple calls, ensuring they have the correct data to work on, and gathering up the results.

    Update Based on Comments

    In your situation, If the operation required to calculate each member of the vals is different then the user would either have to parameterise the my_func() by the index required; as you suggested double my_func(const mxArray *point, const unsigned & index), note how it now returns a single double value as opposed to the whole result array. Or provide a different my_func() for each index; double my_func_n(const mxArray *point).

    You could then call this function or set of functions from as many different threads as you like and get a single result for further computation. We are ignoring many concurrency issues however to do with reading/writing data simultaneously which need thinking about.

    General Mutlitasking Advice

    Before looking into multitasking with your GPU have a look at standard multithreading on a CPU (I recommend Boost Thread Libraries to help: http://www.boost.org/). Once you see how threads are created and used you may find you better understand what you can do with them and how you’d go about doing it.

    Multitasking with a GPU becomes more useful if you are applying mathematical functions to very large matrices or vectors and it is possible to use hardware implementations of certain graphical functions to achieve the mathematical result. There are further libraries to support GPGPU (General Purpose GPU) programming, such as OpenCL, Nvidia’s CUDA, or ATI’s Stream. Have a look at what these libraries provide to give you an idea of how applicable they are to your situation.

    • 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.