// Suma_Uno.cu : Defines the entry point for the console application.
//


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

// Sum 1 to each element in the vector
__global__  void suma1(float * d_vector, int N)
{
	int index = blockIdx.x*blockDim.x + threadIdx.x;
	if(index<N){
		//printf("\n%d ", threadIdx.x);
		d_vector[index] += 1.0f;
	}
}

int main(void)
{
	int lengthvector = 24; // Vector size
	int block_size=8;
	dim3 threads(block_size,1,1);  // Threads per block
	int n_blocks=lengthvector/threads.x;
	//int n_blocks=lengthvector/threads.x+(lengthvector % threads.x == 0 ? 0:1);			
	dim3 grid(n_blocks,1,1); //Grid of blocks of threads

	size_t memSize = lengthvector*sizeof(float);

	// Host memory allocation
	float* vector_h = (float*) malloc(memSize);

	// Vector inicialization
	for(int i=0;i<lengthvector;i++) {
		vector_h[i] = (float)(i%10);
	}
	printf("Antes: \n");
	for(int i=0;i<lengthvector;i++) { // Display the vector
		printf("%f ", vector_h[i]);
	}

	float *vector_d;  // Device vector

	// Device memory allocation
	cudaMalloc((void**)&vector_d, memSize );
	

	// Copy memory from host to device
	cudaMemcpy(vector_d, vector_h, memSize, cudaMemcpyHostToDevice);

	// Call kernel
	
	suma1<<<grid,threads>>> (vector_d,lengthvector);

	// Copy memory from device to host 
	cudaMemcpy(vector_h, vector_d, memSize, cudaMemcpyDeviceToHost);
;

	printf("\n\nDespues: \n");
	for(int i=0;i<lengthvector;i++) { // Display the vector
		printf("%f ", vector_h[i]);
	}

	// Free device memory
	cudaFree(vector_d);
	// Free host memory
	free(vector_h);
	return(0);
}

