多字節數據在內存、顯存(N卡)和寄存器(N卡)存放是按照 “高字節->低字節(bit 31 --------> bit 0)”存放的,稱為小端或小尾。
例如:
char4類型的數據(4個字節),通過分量名訪問:
char4 dog;
dog.x ... 第1個字節
dog.y .... 第2個字節
dog.z ..... 第3個字節
doz.w ...... 第4個字節
存放方式:w/z/y/x(這種方式即小尾或小端,N卡是這樣)。
另一個例子,例如:
char dog[4];
dog[0] = 0x11;
dog[1] = 0x22;
dog[2] = 0x33;
dog[4] = 0x44;
int cat = *(int *)dog;
cat的值是0x44332211,而不是0x11223344。如果使用分量訪問char4中的每個字節,則 可以不管它的存放方式。
如果需要使用絕對位置(例如移位),需要注意:
例如:
uint32_t a = *p;
a & 0xff 第1個字節
(a >> 8) & 0xff 第2個
(a >> 16) & 0xff 第3個
(a >> 24) & 0xff 第4個。
下面是測試代碼:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include
cudaError_t addWithCuda(int *c,char *b, unsigned int size);
__global__ void addKernel(int *c, char *b)
{
//int i = threadIdx.x;
//c[i] = a[i] + b[i];
char dog[4];
dog[0]=0x12;
dog[1]=0x34;
dog[2]=0x56;
dog[3]=0x78;
int *temp = (int *)dog;
c[0] = *temp;
//
int dog2 = 0x12345678;
char *q = (char *)&dog2;
b[0] = q[0];
b[1] = q[1];
b[2] = q[2];
b[3] = q[3];
}
int main()
{
const int arraySize = 5;
char b[arraySize] = { 0 };
int c[arraySize] = { 0 };
printf("cpu : \n");
char dog[4];
dog[0]=0x12;dog[1]=0x34;dog[2]=0x56;dog[3]=0x78;
c[0] = *(int *)dog;
printf("a array dog of char type : {%x,%x,%x,%x} \n",dog[0],dog[1],dog[2],dog[3]);
printf("the address of it's element : {%x,%x,%x,%x} \n",&dog[0],&dog[1],&dog[2],&dog[3]);
printf("we convert the array dog to a int data \n");
printf("c : %x , address: %x \n",c[0],&c[0]);
printf("\n");
int p=0x12345678;//在內存中對應&p對應的地址比如0x04000000開始的12 34 56 78四個字節
char *q;
q=(char *)&p;//&p本來是int *類型,強制轉換為類型char *。此時q的值為0x04000000
//此時q[0]==0x12,q[1]==0x34,q[2]==0x56,q[0]==0x78
printf("a data of type int p = %x\n",p);
printf("we convert the int data to a array of char type \n");
printf("char p = {%x,%x,%x,%x} \n",q[0], q[1], q[2], q[3]);
printf("address of p : %x,%x,%x,%x \n",&q[0], &q[1], &q[2], &q[3]);
printf("\n");
printf("gpu : \n");
c[0] = 0;
// Add vectors in parallel.
cudaError_t cudaStatus = addWithCuda(c, b, arraySize);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addWithCuda failed!");
return 1;
}
printf("array dog : {%x,%x,%x,%x} \n",dog[0],dog[1],dog[2],dog[3]);
printf("c : %x , address: %x \n",c[0],&c[0]);
printf("\n");
printf(" int p : %x \n",0x12345678);
printf("char p : {%x,%x,%x,%x}",b[0],b[1],b[2],b[3]);
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
getchar();
return 0;
}
// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, char *b, unsigned int size)
{
char *dev_b = 0;
int *dev_c = 0;
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(char));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, 1>>>(dev_c,dev_b);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
cudaStatus = cudaMemcpy(b, dev_b, size * sizeof(char), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
Error:
cudaFree(dev_c);
cudaFree(dev_b);
return cudaStatus;
}