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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:23:07+00:00 2026-05-26T04:23:07+00:00

Hello I have this loop in C+, and I was trying to convert it

  • 0

Hello I have this loop in C+, and I was trying to convert it to thrust but without getting the same results…
Any ideas?
thank you

C++ Code

for (i=0;i<n;i++) 
    for (j=0;j<n;j++) 
      values[i]=values[i]+(binv[i*n+j]*d[j]);

Thrust Code

thrust::fill(values.begin(), values.end(), 0);
thrust::transform(make_zip_iterator(make_tuple(
                thrust::make_permutation_iterator(values.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexDivFunctor(n))),
                binv.begin(),
                thrust::make_permutation_iterator(d.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexModFunctor(n))))),
                make_zip_iterator(make_tuple(
                thrust::make_permutation_iterator(values.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexDivFunctor(n))) + n,
                binv.end(),
                thrust::make_permutation_iterator(d.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexModFunctor(n))) + n)),
                thrust::make_permutation_iterator(values.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexDivFunctor(n))),
                function1()
                );

Thrust Functions

struct IndexDivFunctor: thrust::unary_function<int, int>
{
  int n;

  IndexDivFunctor(int n_) : n(n_) {}

  __host__ __device__
  int operator()(int idx)
  {
    return idx / n;
  }
};

struct IndexModFunctor: thrust::unary_function<int, int>
{
  int n;

  IndexModFunctor(int n_) : n(n_) {}

  __host__ __device__
  int operator()(int idx)
  {
    return idx % n;
  }
};


struct function1
{
  template <typename Tuple>
  __host__ __device__
  double operator()(Tuple v)
  {
    return thrust::get<0>(v) + thrust::get<1>(v) * thrust::get<2>(v);
  }
};
  • 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-26T04:23:08+00:00Added an answer on May 26, 2026 at 4:23 am

    To begin with, some general comments. Your loop

    for (i=0;i<n;i++) 
        for (j=0;j<n;j++) 
          v[i]=v[i]+(B[i*n+j]*d[j]);
    

    is the equivalent of the standard BLAS gemv operation

    enter image description here

    where the matrix is stored in row major order. The optimal way to do this on the device would be using CUBLAS, not something constructed out of thrust primitives.

    Having said that, there is absolutely no way the thrust code you posted is ever going to do what your serial code does. The errors you are seeing are not as a result of floating point associativity. Fundamentally thrust::transform applies the functor supplied to every element of the input iterator and stores the result on the output iterator. To yield the same result as the loop you posted, the thrust::transform call would need to perform (n*n) operations of the fmad functor you posted. Clearly it does not. Further, there is no guarantee that thrust::transform would perform the summation/reduction operation in a fashion that would be safe from memory races.

    The correct solution is probably going to be something like:

    1. Use thrust::transform to compute the (n*n) products of the elements of B and d
    2. Use thrust::reduce_by_key to reduce the products into partial sums, yielding Bd
    3. Use thrust::transform to add the resulting matrix-vector product to v to yield the final result.

    In code, firstly define a functor like this:

    struct functor
    {
      template <typename Tuple>
      __host__ __device__
      double operator()(Tuple v)
      {
        return thrust::get<0>(v) * thrust::get<1>(v);
      }
    };
    

    Then do the following to compute the matrix-vector multiplication

      typedef thrust::device_vector<int> iVec;
      typedef thrust::device_vector<double> dVec;
    
      typedef thrust::counting_iterator<int> countIt;
      typedef thrust::transform_iterator<IndexDivFunctor, countIt> columnIt;
      typedef thrust::transform_iterator<IndexModFunctor, countIt> rowIt;
    
      // Assuming the following allocations on the device
      dVec B(n*n), v(n), d(n);
    
      // transformation iterators mapping to vector rows and columns
      columnIt cv_begin = thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexDivFunctor(n));
      columnIt cv_end   = cv_begin + (n*n);
    
      rowIt rv_begin = thrust::make_transform_iterator(thrust::make_counting_iterator(0), IndexModFunctor(n));
      rowIt rv_end   = rv_begin + (n*n);
    
      dVec temp(n*n);
      thrust::transform(make_zip_iterator(
                          make_tuple(
                            B.begin(),
                            thrust::make_permutation_iterator(d.begin(),rv_begin) ) ),
                        make_zip_iterator(
                          make_tuple(
                            B.end(),
                            thrust::make_permutation_iterator(d.end(),rv_end) ) ),
                        temp.begin(),
                        functor());
    
      iVec outkey(n);
      dVec Bd(n);
      thrust::reduce_by_key(cv_begin, cv_end, temp.begin(), outkey.begin(), Bd.begin());
      thrust::transform(v.begin(), v.end(), Bd.begin(), v.begin(), thrust::plus<double>());
    

    Of course, this is a terribly inefficient way to do the computation compared to using a purpose designed matrix-vector multiplication code like dgemv from CUBLAS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have this foreach loop that gives weird result , it displays only
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
Hello I have this list that i am working on. When i move the
I have this kinda template text : Hello {#Name#}, Thanks for coming blah on
Let's say I have this string: Hello &, how are you? I'm fine! Is
i have this: <div id=parent style=overflow:auto> ... <div id=test>Hello</div> ... </div> My question is:
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Problem Hello all! I have this code which takes my jpg image loops through
Lets say I have this code: <?php class hello { var $greeting = hello;
Let's say that I have this string: s = '<p>Hello!</p>' When I pass this

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.