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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:17:12+00:00 2026-06-18T09:17:12+00:00

I am trying to set only the first element of an array to 5.0

  • 0

I am trying to set only the first element of an array to 5.0 (say). ie, just one of the thread shall set a values and rest of the other threads don’t do anything.

Here is my complete code

#include <stdio.h>
#include <cuda.h>

#define GPUERRCHK(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
   if (code != cudaSuccess)
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

void writeBuf( char * fName, float * out_frame, int dim )
{
    FILE * fp = fopen( fName, "w+" );
    int baseIndx = 0;
    for( int i=0 ; i<dim ; i++ )
    {
        for( int j=0 ; j<dim ; j++ )
        {
            fprintf( fp, "%f ", out_frame[ dim + j ] );
        }
        baseIndx += dim;
        fprintf( fp, "\n" );
    }
    fclose( fp );

}

__global__ void kernel( float * s1, float * s2, int dim, int * hx, int *hy, float *hT, int nHeaters )
{
    int x = threadIdx.x + blockIdx.x*blockDim.x;
    int y = threadIdx.y + blockIdx.y*blockDim.y;
    int offset = x + y*blockDim.x*gridDim.x;


    if( offset < 1 )
    {
    s2[0] = 1.0;
    }

    __syncthreads();

}



int main()
{
    srand48( time(NULL) );
    int dim = 1024;

    float *dev_s1, *dev_s2;
    GPUERRCHK( cudaMalloc( (void**)&dev_s1, dim*dim * sizeof(float) ));
    GPUERRCHK( cudaMalloc( (void**)&dev_s2, dim*dim * sizeof(float) ));
    GPUERRCHK( cudaMemset( dev_s1, 0x00, dim*dim * sizeof(float) ));
    GPUERRCHK( cudaMemset( dev_s2, 0x00, dim*dim * sizeof(float) ));



    //heaters
    int *dev_hx, *dev_hy;
    float *dev_hT;
    int nHeaters = 20;
    GPUERRCHK( cudaMalloc( (void**)&dev_hx, nHeaters * sizeof(int) ));
    GPUERRCHK( cudaMalloc( (void**)&dev_hy, nHeaters * sizeof(int) ));
    GPUERRCHK( cudaMalloc( (void**)&dev_hT, nHeaters * sizeof(float) ));


    //init heaters on cpu
    int * hx, *hy;
    float * hT;
    hx = (int*) malloc( nHeaters * sizeof(int) );
    hy = (int*) malloc( nHeaters * sizeof(int) );
    hT = (float*) malloc( nHeaters * sizeof(float) );
    for( int i=0 ; i<nHeaters ; i++ )
    {
    hx[i] = (int) ((float)drand48() * (float)dim) + 5;
    hy[i] = (int) (drand48() * dim) + 5;
    hT[i] = (float) (drand48() * 100) + 50;
    }

    //transfer hx, hy, hT to GPU
    GPUERRCHK( cudaMemcpy( dev_hx, hx, nHeaters * sizeof(int), cudaMemcpyHostToDevice ));
    GPUERRCHK( cudaMemcpy( dev_hy, hy, nHeaters * sizeof(int), cudaMemcpyHostToDevice ));
    GPUERRCHK( cudaMemcpy( dev_hT, hT, nHeaters * sizeof(float), cudaMemcpyHostToDevice ));


    float *out_frame = (float *) malloc( dim*dim*sizeof(float) );


    // run kernel
    int nThreadsPerBlock = 16;
    int nBlockX = (dim+nThreadsPerBlock-1)/nThreadsPerBlock;
    int nBlockY = (dim+nThreadsPerBlock-1)/nThreadsPerBlock;
    kernel<<< dim3(nBlockX, nBlockY), dim3(nThreadsPerBlock, nThreadsPerBlock) >>>( dev_s1, dev_s2, dim, dev_hx, dev_hy, dev_hT, nHeaters );
    GPUERRCHK( cudaPeekAtLastError() );
    GPUERRCHK( cudaDeviceSynchronize() );


    // collect result
    GPUERRCHK( cudaMemcpy( out_frame, dev_s2, dim*dim * sizeof(float), cudaMemcpyDeviceToHost ) );


    int f=1;
    char fName[100];
    snprintf( fName, 100, "out/file_%04d.data", f );
    writeBuf( fName, out_frame, dim );


    cudaFree( dev_s1 );
    cudaFree( dev_s2 );

    free( out_frame );
}

When I run this, the file contains all zeros. How do I achieve what I plan to achieve?
What could be the 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-06-18T09:17:13+00:00Added an answer on June 18, 2026 at 9:17 am

    Your problem is in writeBuf(..) @line:

    fprintf( fp, "%f ", out_frame[ dim + j ] );
    

    dim is 1024 and you are accessing elements from 1024 to 1024+dim-1 and that is why you never see the first element.
    The correct line should be:

    fprintf( fp, "%f ", out_frame[ baseIndx + j ] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to grab only the first instance of an element from an
I'm trying to set a TwoWay binding to a combo box using only a
I have a repository where I'm trying to set up result caching. I've only
trying to display data only after variables have been set. $(document).ready(function () { function
Trying to set it so if a certain condition is met then one of
I'm trying to implement an MPI program to iteratively set each element in an
I'm trying to output all first tds in all table rows, but .html() only
I'm trying to make an array out of a set of div's on a
I'm trying to pullout values from a uint8_t array. But I'm having troubles understanding
Im trying set the single table inheritance model type in a form. So i

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.