Thrust/CUDA replicate an array multiple times combined with the values of another array










1















Let's say I have two arrays



A = 1, 2, 3



and



B = 10,20,30,40,50



I want to generate a new array which would have a size of



sizeof(A) * sizeof(B)



I want to replicate B sizeof(A) times, and on each repetition i, the resultant array should have A[i] added to it. So the result would be something like



11,21,31,41,51,12,22,32,42,52,13,23,33,43,53










share|improve this question


























    1















    Let's say I have two arrays



    A = 1, 2, 3



    and



    B = 10,20,30,40,50



    I want to generate a new array which would have a size of



    sizeof(A) * sizeof(B)



    I want to replicate B sizeof(A) times, and on each repetition i, the resultant array should have A[i] added to it. So the result would be something like



    11,21,31,41,51,12,22,32,42,52,13,23,33,43,53










    share|improve this question
























      1












      1








      1








      Let's say I have two arrays



      A = 1, 2, 3



      and



      B = 10,20,30,40,50



      I want to generate a new array which would have a size of



      sizeof(A) * sizeof(B)



      I want to replicate B sizeof(A) times, and on each repetition i, the resultant array should have A[i] added to it. So the result would be something like



      11,21,31,41,51,12,22,32,42,52,13,23,33,43,53










      share|improve this question














      Let's say I have two arrays



      A = 1, 2, 3



      and



      B = 10,20,30,40,50



      I want to generate a new array which would have a size of



      sizeof(A) * sizeof(B)



      I want to replicate B sizeof(A) times, and on each repetition i, the resultant array should have A[i] added to it. So the result would be something like



      11,21,31,41,51,12,22,32,42,52,13,23,33,43,53







      cuda thrust






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 8:47









      strandedstranded

      1356




      1356






















          1 Answer
          1






          active

          oldest

          votes


















          2














          This task can be interpreted as a 2-dimensional problem where the output array can be treated as a matrix of dimensions sizeof(A) times sizeof(B). In this way, we can use 2D CUDA indexing to achieve the desired functionality. A sample CUDA C++ code of this 2D implementation is shown below:



          #include <iostream>
          #include <cuda_runtime.h>
          #include <cassert>

          using namespace std;

          __global__ void kernel_replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          const int ai = blockIdx.x * blockDim.x + threadIdx.x;
          const int bi = blockIdx.y * blockDim.y + threadIdx.y;

          if(ai<alen && bi<blen)

          const int ci = ai * blen + bi;
          c[ci] = a[ai] + b[bi];




          void replicate_device(int* a, int* b, int* c, int alen, int blen, int clen)

          dim3 block(16,16);
          dim3 grid;
          grid.x = (alen + block.x - 1) / block.x;
          grid.y = (blen + block.y - 1) / block.y;

          kernel_replicate<<<grid, block>>>(a,b,c,alen,blen,clen);

          assert(cudaSuccess == cudaDeviceSynchronize());



          void replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          int *ad, *bd, *cd;

          size_t abytes = alen * sizeof(int);
          size_t bbytes = blen * sizeof(int);
          size_t cbytes = clen * sizeof(int);

          cudaMalloc(&ad, abytes);
          cudaMalloc(&bd, bbytes);
          cudaMalloc(&cd, cbytes);

          cudaMemcpy(ad,a, abytes, cudaMemcpyHostToDevice);
          cudaMemcpy(bd,b, bbytes, cudaMemcpyHostToDevice);

          replicate_device(ad,bd,cd, alen,blen,clen);

          cudaMemcpy(c,cd, cbytes, cudaMemcpyDeviceToHost);


          cudaFree(ad);
          cudaFree(bd);
          cudaFree(cd);



          int main()

          const int alen = 3;
          const int blen = 5;
          const int clen = alen * blen;

          int A[alen] = 1,2,3;
          int B[blen] = 10,20,30,40,50;
          int C[clen] = 0;

          replicate(A,B,C,alen, blen, clen);

          for(int i=0; i<alen; i++)

          cout<<A[i]<<" ";

          cout<<endl;

          for(int i=0; i<blen; i++)

          cout<<B[i]<<" ";

          cout<<endl;

          for(int i=0; i<clen; i++)

          cout<<C[i]<<" ";

          cout<<endl;


          return 0;






          share|improve this answer























          • This is exactly what I wanted, thanks!

            – stranded
            Nov 14 '18 at 10:04










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296134%2fthrust-cuda-replicate-an-array-multiple-times-combined-with-the-values-of-anothe%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          This task can be interpreted as a 2-dimensional problem where the output array can be treated as a matrix of dimensions sizeof(A) times sizeof(B). In this way, we can use 2D CUDA indexing to achieve the desired functionality. A sample CUDA C++ code of this 2D implementation is shown below:



          #include <iostream>
          #include <cuda_runtime.h>
          #include <cassert>

          using namespace std;

          __global__ void kernel_replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          const int ai = blockIdx.x * blockDim.x + threadIdx.x;
          const int bi = blockIdx.y * blockDim.y + threadIdx.y;

          if(ai<alen && bi<blen)

          const int ci = ai * blen + bi;
          c[ci] = a[ai] + b[bi];




          void replicate_device(int* a, int* b, int* c, int alen, int blen, int clen)

          dim3 block(16,16);
          dim3 grid;
          grid.x = (alen + block.x - 1) / block.x;
          grid.y = (blen + block.y - 1) / block.y;

          kernel_replicate<<<grid, block>>>(a,b,c,alen,blen,clen);

          assert(cudaSuccess == cudaDeviceSynchronize());



          void replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          int *ad, *bd, *cd;

          size_t abytes = alen * sizeof(int);
          size_t bbytes = blen * sizeof(int);
          size_t cbytes = clen * sizeof(int);

          cudaMalloc(&ad, abytes);
          cudaMalloc(&bd, bbytes);
          cudaMalloc(&cd, cbytes);

          cudaMemcpy(ad,a, abytes, cudaMemcpyHostToDevice);
          cudaMemcpy(bd,b, bbytes, cudaMemcpyHostToDevice);

          replicate_device(ad,bd,cd, alen,blen,clen);

          cudaMemcpy(c,cd, cbytes, cudaMemcpyDeviceToHost);


          cudaFree(ad);
          cudaFree(bd);
          cudaFree(cd);



          int main()

          const int alen = 3;
          const int blen = 5;
          const int clen = alen * blen;

          int A[alen] = 1,2,3;
          int B[blen] = 10,20,30,40,50;
          int C[clen] = 0;

          replicate(A,B,C,alen, blen, clen);

          for(int i=0; i<alen; i++)

          cout<<A[i]<<" ";

          cout<<endl;

          for(int i=0; i<blen; i++)

          cout<<B[i]<<" ";

          cout<<endl;

          for(int i=0; i<clen; i++)

          cout<<C[i]<<" ";

          cout<<endl;


          return 0;






          share|improve this answer























          • This is exactly what I wanted, thanks!

            – stranded
            Nov 14 '18 at 10:04















          2














          This task can be interpreted as a 2-dimensional problem where the output array can be treated as a matrix of dimensions sizeof(A) times sizeof(B). In this way, we can use 2D CUDA indexing to achieve the desired functionality. A sample CUDA C++ code of this 2D implementation is shown below:



          #include <iostream>
          #include <cuda_runtime.h>
          #include <cassert>

          using namespace std;

          __global__ void kernel_replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          const int ai = blockIdx.x * blockDim.x + threadIdx.x;
          const int bi = blockIdx.y * blockDim.y + threadIdx.y;

          if(ai<alen && bi<blen)

          const int ci = ai * blen + bi;
          c[ci] = a[ai] + b[bi];




          void replicate_device(int* a, int* b, int* c, int alen, int blen, int clen)

          dim3 block(16,16);
          dim3 grid;
          grid.x = (alen + block.x - 1) / block.x;
          grid.y = (blen + block.y - 1) / block.y;

          kernel_replicate<<<grid, block>>>(a,b,c,alen,blen,clen);

          assert(cudaSuccess == cudaDeviceSynchronize());



          void replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          int *ad, *bd, *cd;

          size_t abytes = alen * sizeof(int);
          size_t bbytes = blen * sizeof(int);
          size_t cbytes = clen * sizeof(int);

          cudaMalloc(&ad, abytes);
          cudaMalloc(&bd, bbytes);
          cudaMalloc(&cd, cbytes);

          cudaMemcpy(ad,a, abytes, cudaMemcpyHostToDevice);
          cudaMemcpy(bd,b, bbytes, cudaMemcpyHostToDevice);

          replicate_device(ad,bd,cd, alen,blen,clen);

          cudaMemcpy(c,cd, cbytes, cudaMemcpyDeviceToHost);


          cudaFree(ad);
          cudaFree(bd);
          cudaFree(cd);



          int main()

          const int alen = 3;
          const int blen = 5;
          const int clen = alen * blen;

          int A[alen] = 1,2,3;
          int B[blen] = 10,20,30,40,50;
          int C[clen] = 0;

          replicate(A,B,C,alen, blen, clen);

          for(int i=0; i<alen; i++)

          cout<<A[i]<<" ";

          cout<<endl;

          for(int i=0; i<blen; i++)

          cout<<B[i]<<" ";

          cout<<endl;

          for(int i=0; i<clen; i++)

          cout<<C[i]<<" ";

          cout<<endl;


          return 0;






          share|improve this answer























          • This is exactly what I wanted, thanks!

            – stranded
            Nov 14 '18 at 10:04













          2












          2








          2







          This task can be interpreted as a 2-dimensional problem where the output array can be treated as a matrix of dimensions sizeof(A) times sizeof(B). In this way, we can use 2D CUDA indexing to achieve the desired functionality. A sample CUDA C++ code of this 2D implementation is shown below:



          #include <iostream>
          #include <cuda_runtime.h>
          #include <cassert>

          using namespace std;

          __global__ void kernel_replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          const int ai = blockIdx.x * blockDim.x + threadIdx.x;
          const int bi = blockIdx.y * blockDim.y + threadIdx.y;

          if(ai<alen && bi<blen)

          const int ci = ai * blen + bi;
          c[ci] = a[ai] + b[bi];




          void replicate_device(int* a, int* b, int* c, int alen, int blen, int clen)

          dim3 block(16,16);
          dim3 grid;
          grid.x = (alen + block.x - 1) / block.x;
          grid.y = (blen + block.y - 1) / block.y;

          kernel_replicate<<<grid, block>>>(a,b,c,alen,blen,clen);

          assert(cudaSuccess == cudaDeviceSynchronize());



          void replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          int *ad, *bd, *cd;

          size_t abytes = alen * sizeof(int);
          size_t bbytes = blen * sizeof(int);
          size_t cbytes = clen * sizeof(int);

          cudaMalloc(&ad, abytes);
          cudaMalloc(&bd, bbytes);
          cudaMalloc(&cd, cbytes);

          cudaMemcpy(ad,a, abytes, cudaMemcpyHostToDevice);
          cudaMemcpy(bd,b, bbytes, cudaMemcpyHostToDevice);

          replicate_device(ad,bd,cd, alen,blen,clen);

          cudaMemcpy(c,cd, cbytes, cudaMemcpyDeviceToHost);


          cudaFree(ad);
          cudaFree(bd);
          cudaFree(cd);



          int main()

          const int alen = 3;
          const int blen = 5;
          const int clen = alen * blen;

          int A[alen] = 1,2,3;
          int B[blen] = 10,20,30,40,50;
          int C[clen] = 0;

          replicate(A,B,C,alen, blen, clen);

          for(int i=0; i<alen; i++)

          cout<<A[i]<<" ";

          cout<<endl;

          for(int i=0; i<blen; i++)

          cout<<B[i]<<" ";

          cout<<endl;

          for(int i=0; i<clen; i++)

          cout<<C[i]<<" ";

          cout<<endl;


          return 0;






          share|improve this answer













          This task can be interpreted as a 2-dimensional problem where the output array can be treated as a matrix of dimensions sizeof(A) times sizeof(B). In this way, we can use 2D CUDA indexing to achieve the desired functionality. A sample CUDA C++ code of this 2D implementation is shown below:



          #include <iostream>
          #include <cuda_runtime.h>
          #include <cassert>

          using namespace std;

          __global__ void kernel_replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          const int ai = blockIdx.x * blockDim.x + threadIdx.x;
          const int bi = blockIdx.y * blockDim.y + threadIdx.y;

          if(ai<alen && bi<blen)

          const int ci = ai * blen + bi;
          c[ci] = a[ai] + b[bi];




          void replicate_device(int* a, int* b, int* c, int alen, int blen, int clen)

          dim3 block(16,16);
          dim3 grid;
          grid.x = (alen + block.x - 1) / block.x;
          grid.y = (blen + block.y - 1) / block.y;

          kernel_replicate<<<grid, block>>>(a,b,c,alen,blen,clen);

          assert(cudaSuccess == cudaDeviceSynchronize());



          void replicate(int* a, int* b, int* c, int alen, int blen, int clen)

          int *ad, *bd, *cd;

          size_t abytes = alen * sizeof(int);
          size_t bbytes = blen * sizeof(int);
          size_t cbytes = clen * sizeof(int);

          cudaMalloc(&ad, abytes);
          cudaMalloc(&bd, bbytes);
          cudaMalloc(&cd, cbytes);

          cudaMemcpy(ad,a, abytes, cudaMemcpyHostToDevice);
          cudaMemcpy(bd,b, bbytes, cudaMemcpyHostToDevice);

          replicate_device(ad,bd,cd, alen,blen,clen);

          cudaMemcpy(c,cd, cbytes, cudaMemcpyDeviceToHost);


          cudaFree(ad);
          cudaFree(bd);
          cudaFree(cd);



          int main()

          const int alen = 3;
          const int blen = 5;
          const int clen = alen * blen;

          int A[alen] = 1,2,3;
          int B[blen] = 10,20,30,40,50;
          int C[clen] = 0;

          replicate(A,B,C,alen, blen, clen);

          for(int i=0; i<alen; i++)

          cout<<A[i]<<" ";

          cout<<endl;

          for(int i=0; i<blen; i++)

          cout<<B[i]<<" ";

          cout<<endl;

          for(int i=0; i<clen; i++)

          cout<<C[i]<<" ";

          cout<<endl;


          return 0;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 9:41









          sgarizvisgarizvi

          12.6k84479




          12.6k84479












          • This is exactly what I wanted, thanks!

            – stranded
            Nov 14 '18 at 10:04

















          • This is exactly what I wanted, thanks!

            – stranded
            Nov 14 '18 at 10:04
















          This is exactly what I wanted, thanks!

          – stranded
          Nov 14 '18 at 10:04





          This is exactly what I wanted, thanks!

          – stranded
          Nov 14 '18 at 10:04



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296134%2fthrust-cuda-replicate-an-array-multiple-times-combined-with-the-values-of-anothe%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20