#include <iostream>

#define RANGE(I, FROM, TO) size_t I = FROM; I < TO; I += 1

//
// example: CUDA_CHECK( cudaMalloc(dx, x, N*sizeof(int) );
//
#define CUDA_CHECK(code) { cuda_check((code), __FILE__, __LINE__); }
inline void cuda_check(cudaError_t code, const char *file, int line) {
    if(code != cudaSuccess) {
        std::cout << file << ':' << line << ": [CUDA ERROR] " << cudaGetErrorString(code) << std::endl; 
        std::abort();
    }
}


// step 04


__global__ void add(int N, const int* dx, int* dy) {
    size_t index = blockIdx.x * blockDim.x + threadIdx.x;
    if (index > N) return;
    dy[index] += dx[index];
}




int main()
{
    constexpr int N = 1000;
    int* x = (int*)malloc(N*sizeof(int));
    int* y = (int*)malloc(N*sizeof(int));
    for(int i = 0; i < N; ++i) {
        x[i] = i;
        y[i] = i*i;
    }

    // step 05
    int* dx;
    int* dy;

    // 1. allocate on device
    size_t size = N * sizeof(int);
    cudaMalloc(&dx, size);
    cudaMalloc(&dy, size);

    // 2. copy from host to device
    cudaMemcpy(dx, x, size, cudaMemcpyHostToDevice);
    cudaMemcpy(dy, y, size, cudaMemcpyHostToDevice);

    // 3. launch CUDA kernel
    const int threads_per_bloc = 32;
    add<<<N, 32>>>(N, dx, dy);
    cudaDeviceSynchronize();


    // 4. copy result from device to host
    cudaMemcpy(y, dy, size, cudaMemcpyDeviceToHost);

    // 5. free device memory
    cudaFree(dx);
    cudaFree(dy);

    // checking results
    bool ok = true;
    for(int i = 0; i < N; ++i) {
        const int expected_result = i + i*i;
        if(y[i] != expected_result) {
            std::cout << "Failure" << std::endl;
            std::cout << "Result at index i=" 
                << i << ": expected " 
                << i << '+' << i*i << '=' << expected_result << ", got " << y[i] << std::endl;
            ok = false;
            break;
        }
    }
    if(ok) std::cout << "Success" << std::endl;

    free(x);
    free(y);

    return 0;
}

// # Question 3
// Pour une suite de N tâches, avec des blocs de 32 threads
// - il faudra idéalement ceil(N/32) blocs.
// - sur le dernier bloc, N % 32 threads exécuteront une tâche
// - sur le dernier bloc, 32 - (N % 32) threads n'exécuteront aucune tâche
