#include <amsculib2/amsculib2.hpp>

namespace amscuda
{

__global__ void test_amscurarray1_kf1(curarray<int> *q)
{
    int I,J,Na;
    int I0 = threadIdx.x + blockIdx.x*blockDim.x;
    int Is = blockDim.x*gridDim.x;
    int N = q->Narrays;

    for(I=I0; I<N;I = I + Is)
    {
        Na = q->N[I];

        //printf("I:%d Na: %d\n",I,Na);
        //q->dev_resizearray(I, Na);
        for(J=0;J<Na;J++)
        {
            q->devarrayptrs[I][J] = J;
        }
    }
}

void test_amscurarray1()
{
    int I;
    cudaError_t err = cudaSuccess;

    printf("test_amscurarray1:\n");
    curarray<int> *qarray = NULL;

    curarray_new(&qarray,100);
    err = cudaGetLastError();
    if(err!=cudaSuccess)
    {
        printf("debug error trap 1: %s\n",cudaGetErrorString(err));
    }

    for(I=0;I<100;I++)
    {
        qarray->resizearray(I,5);
    }

    qarray->push();
    qarray->pull();

    cuda_errortrap("debug: error trap 2");

    for(I=0;I<5;I++)
    {
        printf("array[%d], size %d\n",I,qarray->N[I]);
    }

    //
    for(I=0;I<100;I++)
    {
        qarray->resizearray(I,I%5);
        cuda_errortrap("debug: error trap resize2");
    }
    
    qarray->push();
    qarray->pull();
    test_amscurarray1_kf1<<<128,1>>>(qarray->devptr);

    cuda_errortrap("debug: error trap kf1");

    qarray->pull();
    cuda_errortrap("debug: error trap pull2");


    for(I=0;I<5;I++)
    {
        printf("array[%d], size %d\n",I,qarray->N[I]);
    }

    curarray_delete(&qarray);
    
    cuda_errortrap("debug: error trap 3");

    return;
}

};