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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:31:19+00:00 2026-06-14T17:31:19+00:00

I’m trying to test out a sample code from the CUDA site http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#kernels .

  • 0

I’m trying to test out a sample code from the CUDA site http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#kernels.

I simply want to add two arrays A and B of size 4, and store it in array C. Here is what I have so far:

#include <stdio.h>
#include "util.h"
void print_array(int* array, int size) {
int i;
for (i = 0; i < size; i++) {
    printf("%d ", array[i]);
}
printf("\n");
}

__global__ void VecAdd(int* A, int* B, int* C) {
int i = threadIdx.x;
C[i] = A[i] + B[i];
}

int main(int argc , char **argv) {
int N = 4;
    int i;
int *A = (int *) malloc(N * sizeof(int));
int *B = (int *) malloc(N * sizeof(int));
int *C = (int *) malloc(N * sizeof(int));

for (i = 0; i < N; i++) {
    A[i] = i + 1;
    B[i] = i + 1;
}

print_array(A, N);
print_array(B, N);


VecAdd<<<1, N>>>(A, B, C);
print_array(C, N);
    return 0;
}

I’m expecting the C array (the last row of the output) to be 2, 4, 6, 8, but it doesn’t seem to get added:

1 2 3 4
1 2 3 4
0 0 0 0

What am I missing?

  • 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-06-14T17:31:22+00:00Added an answer on June 14, 2026 at 5:31 pm

    First, you have to define the pointers that will hold the data that will be copied to GPU:

    In your example, we want to copy the arrays ‘a’,’b’ and ‘c’ from CPU to the GPU's global memory.

    int a[array_size], b[array_size],c[array_size]; // your original arrays
    int *a_cuda,*b_cuda,*c_cuda;                    // defining the "cuda" pointers 
    

    define the size that each array will occupy.

    int size = array_size * sizeof(int); // Is the same for the 3 arrays
    

    Then you will allocate the space to the data that will be used in cuda:

    Cuda memory allocation:

    msg_erro[0] = cudaMalloc((void **)&a_cuda,size);
    msg_erro[1] = cudaMalloc((void **)&b_cuda,size);
    msg_erro[2] = cudaMalloc((void **)&c_cuda,size);
    

    Now we need to copy this data from CPU to the GPU:

    Copy from CPU to GPU:

    msg_erro[3] = cudaMemcpy(a_cuda, a,size,cudaMemcpyHostToDevice);
    msg_erro[4] = cudaMemcpy(b_cuda, b,size,cudaMemcpyHostToDevice);
    msg_erro[5] = cudaMemcpy(c_cuda, c,size,cudaMemcpyHostToDevice);
    

    Execute the kernel

    int blocks = //;
    int threads_per_block = //;
    VecAdd<<<blocks, threads_per_block>>>(a_cuda, b_cuda, c_cuda);
    

    Copy the results from GPU to CPU (in our example array C):

    msg_erro[6] = cudaMemcpy(c,c_cuda,size,cudaMemcpyDeviceToHost);
    

    Free Memory:

    cudaFree(a_cuda);
    cudaFree(b_cuda);
    cudaFree(c_cuda);
    

    For debugging purposes, I normally save the status of the functions on an array, like this:

    cudaError_t msg_erro[var];
    

    However, this is not strictly necessary but it will save you time if an error occurs during the allocation or memory transference. You can take out all the ‘msg_erro[x] =’ from the code above if you wish.

    If you mantain the ‘msg_erro[x] =’, and if a error does occur you can use a function like the one that follows, to print these erros:

    void printErros(cudaError_t *erros,int size)
    {
     for(int i = 0; i < size; i++)
          printf("{%d} => %s\n",i ,cudaGetErrorString(erros[i]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:

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.