//
// include files
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <hip/hip_runtime.h>
#include "helper_hip.h"

//
// kernel routine
// 

__global__ void my_first_kernel(float *x)
{
    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    x[tid] = tid;
}

//
// HIP routine to be called by main code
//

extern
int prac6(int nblocks, int nthreads)
{
  float *h_x, *d_x;
  int   nsize, n; 

  // allocate memory for arrays

  nsize = nblocks*nthreads ;

  h_x = (float *)malloc(nsize*sizeof(float));
  checkHipErrors( hipMalloc((void **)&d_x, nsize*sizeof(float)) );

  // execute kernel

  my_first_kernel<<<nblocks,nthreads>>>(d_x);

  // copy back results and print them out

  checkHipErrors( hipMemcpy(h_x,d_x,nsize*sizeof(float),hipMemcpyDefault) );

  for (n=0; n<nsize; n++) printf(" n,  x  =  %d  %f \n",n,h_x[n]);

  // free memory 

  checkHipErrors( hipFree(d_x) );
  free(h_x);

  return 0;
}
