我嘗試將內存中的數據寫入文件。然後將其讀回並寫回到內存中。 目前我有以下代碼。但據我所知,它不能完全工作。內存中的數據是圖像。只要我沒有從文件中讀取它的罰款。但只要我讀到它被破壞了。將內存歸檔回內存
size_t memsize = frameWidth * frameHeight; //calculate the memory size
//Writing characters to file
char test;
char* data = (char*) malloc (memsize); //create a datablock for the data on the heap
//*
cudaMemcpy(data, input, memsize,cudaMemcpyDeviceToHost); //copy the data form the cuda to the CPU
FILE *fp;
fp = fopen (filename, "wb");
int frames = memsize/sizeof(char);
for (int i = 0; i<(frames); i++)
{
test = data[i];
fprintf(fp, "%c",test);
}
fclose (fp);
/**/
free(data); //clear the allocated memory
//////////////////////////////////////
//write back to the memory///////////
////////////////////////////////////
data = (char*) malloc (memsize); //create a datablock for the data on the heap
//*
// FILE *fp;
fp = fopen (filename, "rb"); //read
for(int i=0; i<memsize; i++)
fscanf(fp, "%c ", (data+i));
cudaMemcpy(input, data, memsize,cudaMemcpyHostToDevice); //copy the data form the cuda to the CPU
log_kernel<<<grid, block>>>(input, pitchIn/sizeof(float), output, pitchOut/sizeof(float), frameHeight);
fclose (fp);
/**/
free(data);
//clear the allocated memory
但這並沒有完全奏效。我看到數據被破壞,但我不明白爲什麼有人會看到我犯的錯誤。