Commit 44683b39 by 张裕浩

Update TopK unit test

parent cbe68108
......@@ -105,10 +105,62 @@ bool TestTopK1()
TopK(sUser, tUser2, indexUser2, dim, k);
/* check results */
cpuTest = t1->CheckData(tAnswer1, tUnitNum) && tUser1.CheckData(tAnswer1, tUnitNum)
&& t2->CheckData(tAnswer2, tUnitNum) && tUser2.CheckData(tAnswer2, tUnitNum)
&& index1->CheckData(indexAnswer1, tUnitNum) && indexUser1.CheckData(indexAnswer1, tUnitNum)
&& index2->CheckData(indexAnswer2, tUnitNum) && indexUser2.CheckData(indexAnswer2, tUnitNum);
for (int i = 0; i < tDimSize[1]; ++i)
{
for (int j = 0; j < tDimSize[0]; ++j)
{
float tmpData = ((float *)t1->data)[i + tDimSize[1] * j];
int tmpIndex = ((int *)index1->data)[i + tDimSize[1] * j];
float tmpDataUser = ((float *)tUser1.data)[i + tDimSize[1] * j];
int tmpIndexUser = ((int *)indexUser1.data)[i + tDimSize[1] * j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[0]; ++k)
{
float* ans = tAnswer1[0];
int* ansIndex = indexAnswer1[0];
if (tmpData == ans[i + tDimSize[1] * k] && tmpIndex == ansIndex[i + tDimSize[1] * k])
{
flag = true;
}
if (tmpDataUser == ans[i + tDimSize[1] * k] && tmpIndexUser == ansIndex[i + tDimSize[1] * k])
{
flagUser = true;
}
}
cpuTest = cpuTest&&flag&&flagUser;
}
}
for (int i = 0; i < tDimSize[0]; ++i)
{
for (int j = 0; j < tDimSize[1]; ++j)
{
float tmpData = ((float *)t2->data)[i * tDimSize[1] + j];
int tmpIndex = ((int *)index2->data)[i * tDimSize[1] + j];
float tmpDataUser = ((float *)tUser2.data)[i * tDimSize[1] + j];
int tmpIndexUser = ((int *)indexUser2.data)[i * tDimSize[1] + j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[1]; ++k)
{
float* ans = tAnswer2[0];
int* ansIndex = indexAnswer2[0];
if (tmpData == ans[i * tDimSize[1] + k] && tmpIndex == ansIndex[i * tDimSize[1] + k])
{
flag = true;
}
if (tmpDataUser == ans[i * tDimSize[1] + k] && tmpIndexUser == ansIndex[i * tDimSize[1] + k])
{
flagUser = true;
}
}
cpuTest = cpuTest&&flag&&flagUser;
}
}
#ifdef USE_CUDA
/* GPU test */
......@@ -152,10 +204,74 @@ bool TestTopK1()
TopK(sUserGPU, tUserGPU2, indexUserGPU2, dim, k);
/* check results */
gpuTest = tGPU1->CheckData(tAnswer1, tUnitNum) && tUserGPU1.CheckData(tAnswer1, tUnitNum)
&& tGPU2->CheckData(tAnswer2, tUnitNum) && tUserGPU2.CheckData(tAnswer2, tUnitNum)
&& indexGPU1->CheckData(indexAnswer1, tUnitNum) && indexUserGPU1.CheckData(indexAnswer1, tUnitNum)
&& indexGPU2->CheckData(indexAnswer2, tUnitNum) && indexUserGPU2.CheckData(indexAnswer2, tUnitNum);
float* checkData = new float[tUnitNum];
int* checkIndex = new int[tUnitNum];
float* checkDataUser = new float[tUnitNum];
int* checkIndexUser = new int[tUnitNum];
cudaMemcpy(checkData, tGPU1->data, sizeof(DTYPE)*tUnitNum,cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndex, indexGPU1->data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkDataUser, tUserGPU1.data, sizeof(DTYPE)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndexUser, indexUserGPU1.data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
for (int i = 0; i < tDimSize[1]; ++i)
{
for (int j = 0; j < tDimSize[0]; ++j)
{
float tmpData = ((float *)checkData)[i + tDimSize[1] * j];
int tmpIndex = ((int *)checkIndex)[i + tDimSize[1] * j];
float tmpDataUser = ((float *)checkDataUser)[i + tDimSize[1] * j];
int tmpIndexUser = ((int *)checkIndexUser)[i + tDimSize[1] * j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[0]; ++k)
{
float* ans = tAnswer1[0];
int* ansIndex = indexAnswer1[0];
if (tmpData == ans[i + tDimSize[1] * k] && tmpIndex == ansIndex[i + tDimSize[1] * k])
{
flag = true;
}
if (tmpDataUser == ans[i + tDimSize[1] * k] && tmpIndexUser == ansIndex[i + tDimSize[1] * k])
{
flagUser = true;
}
}
gpuTest = gpuTest&&flag&&flagUser;
}
}
cudaMemcpy(checkData, tGPU2->data, sizeof(DTYPE)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndex, indexGPU2->data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkDataUser, tUserGPU2.data, sizeof(DTYPE)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndexUser, indexUserGPU2.data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
for (int i = 0; i < tDimSize[0]; ++i)
{
for (int j = 0; j < tDimSize[1]; ++j)
{
float tmpData = ((float *)checkData)[i * tDimSize[1] + j];
int tmpIndex = ((int *)checkIndex)[i * tDimSize[1] + j];
float tmpDataUser = ((float *)checkDataUser)[i * tDimSize[1] + j];
int tmpIndexUser = ((int *)checkIndexUser)[i * tDimSize[1] + j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[1]; ++k)
{
float* ans = tAnswer2[0];
int* ansIndex = indexAnswer2[0];
if (tmpData == ans[i * tDimSize[1] + k] && tmpIndex == ansIndex[i * tDimSize[1] + k])
{
flag = true;
}
if (tmpDataUser == ans[i * tDimSize[1] + k] && tmpIndexUser == ansIndex[i * tDimSize[1] + k])
{
flagUser = true;
}
}
gpuTest = gpuTest&&flag&&flagUser;
}
}
/* destroy variables */
delete s;
......@@ -170,6 +286,10 @@ bool TestTopK1()
delete indexGPU2;
delete[] sDimSize;
delete[] tDimSize;
delete[] checkData;
delete[] checkIndex;
delete[] checkDataUser;
delete[] checkIndexUser;
return cpuTest && gpuTest;
#else
......@@ -247,8 +367,33 @@ bool TestTopK2()
TopK(sUser, tUser, indexUser, dim, k);
/* check results */
cpuTest = t->CheckData(tAnswer, tUnitNum) && tUser.CheckData(tAnswer, tUnitNum)
&& index->CheckData(indexAnswer, tUnitNum) && indexUser.CheckData(indexAnswer, tUnitNum);
for (int i = 0; i < tDimSize[0]; ++i)
{
for (int j = 0; j < tDimSize[1]; ++j)
{
float tmpData = ((float *)t->data)[i * tDimSize[1] + j];
int tmpIndex = ((int *)index->data)[i * tDimSize[1] + j];
float tmpDataUser = ((float *)tUser.data)[i * tDimSize[1] + j];
int tmpIndexUser = ((int *)indexUser.data)[i * tDimSize[1] + j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[1]; ++k)
{
float* ans = tAnswer[0];
int* ansIndex = indexAnswer[0];
if (tmpData == ans[i * tDimSize[1] + k] && tmpIndex == ansIndex[i * tDimSize[1] + k])
{
flag = true;
}
if (tmpDataUser == ans[i * tDimSize[1] + k] && tmpIndexUser == ansIndex[i * tDimSize[1] + k])
{
flagUser = true;
}
}
cpuTest = cpuTest&&flag&&flagUser;
}
}
#ifdef USE_CUDA
/* GPU test */
......@@ -279,8 +424,42 @@ bool TestTopK2()
TopK(sUserGPU, tUserGPU, indexUserGPU, dim, k);
/* check results */
gpuTest = tGPU->CheckData(tAnswer, tUnitNum) && tUserGPU.CheckData(tAnswer, tUnitNum)
&& indexGPU->CheckData(indexAnswer, tUnitNum) && indexUserGPU.CheckData(indexAnswer, tUnitNum);
float* checkData = new float[tUnitNum];
int* checkIndex = new int[tUnitNum];
float* checkDataUser = new float[tUnitNum];
int* checkIndexUser = new int[tUnitNum];
cudaMemcpy(checkData, tGPU->data, sizeof(DTYPE)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndex, indexGPU->data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkDataUser, tUserGPU.data, sizeof(DTYPE)*tUnitNum, cudaMemcpyDeviceToHost);
cudaMemcpy(checkIndexUser, indexUserGPU.data, sizeof(int)*tUnitNum, cudaMemcpyDeviceToHost);
for (int i = 0; i < tDimSize[0]; ++i)
{
for (int j = 0; j < tDimSize[1]; ++j)
{
float tmpData = ((float *)checkData)[i * tDimSize[1] + j];
int tmpIndex = ((int *)checkIndex)[i * tDimSize[1] + j];
float tmpDataUser = ((float *)checkDataUser)[i * tDimSize[1] + j];
int tmpIndexUser = ((int *)checkIndexUser)[i * tDimSize[1] + j];
bool flag = false;
bool flagUser = false;
for (int k = 0; k < tDimSize[1]; ++k)
{
float* ans = tAnswer[0];
int* ansIndex = indexAnswer[0];
if (tmpData == ans[i * tDimSize[1] + k] && tmpIndex == ansIndex[i * tDimSize[1] + k])
{
flag = true;
}
if (tmpDataUser == ans[i * tDimSize[1] + k] && tmpIndexUser == ansIndex[i * tDimSize[1] + k])
{
flagUser = true;
}
}
gpuTest = gpuTest&&flag&&flagUser;
}
}
/* destroy variables */
delete s;
......@@ -291,6 +470,10 @@ bool TestTopK2()
delete indexGPU;
delete[] sDimSize;
delete[] tDimSize;
delete[] checkData;
delete[] checkIndex;
delete[] checkDataUser;
delete[] checkIndexUser;
return cpuTest && gpuTest;
#else
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论