how to solve error of misaligned address?

When I code with CUDA in ubuntu16.04. I was confronted with misaligned address error. I was confused with the reason and search on the internet, but no good results can help me.
one problem is as follows:
in foo.cu has a function:
void integration(unsigned char*a, unsigned short s, float d, float trans[]){
float
m_depthIntegration;
uchar4
m_colorIntegration;

cudaSafeCall(cudaMalloc(&m_depthIntegration, sizeof(float)640480));//here is the misaligned address error
cudaSafeCall(cudaMalloc(&m_colorIntegration, sizeof(uchar4)640480));
cudaSafeCall(cudaMemcpy(m_depthIntegration, depth, sizeof(float)640480, cudaMemcpyHostToDevice));
cudaSafeCall(cudaMemcpy(m_colorIntegration, a2, sizeof(uchar4)640480, cudaMemcpyHostToDevice));

}

and i call this function in main.cpp but there is misaligned address error in this statement:cudaSafeCall(cudaMalloc(&m_depthIntegration, sizeof(float)640480));

the other one problem is as follow:
in CUDASceneRepHashSDF.h has a function:
unsigned int getHeapFreeCount() {
unsigned int count;
MLIB_CUDA_SAFE_CALL(cudaMemcpy(&count, m_hashData.d_heapCounter, sizeof(unsigned int), cudaMemcpyDeviceToHost));
return count+1; //there is one more free than the address suggests (0 would be also a valid address)
}

when I call this funtion, there is a misaligned address in this statement:MLIB_CUDA_SAFE_CALL(cudaMemcpy(&count, m_hashData.d_heapCounter, sizeof(unsigned int), cudaMemcpyDeviceToHost));
and m_hashData is a struct HashDataStruct in VoxelUtilHashSDF.h and just like:

struct HashDataStruct {


uint* d_heap; //heap that manages free memory

uint* d_heapCounter;//single element; used as an atomic counter(points to the next free block)

int* d_hashDecision; //
int* d_hashDecisionPrefix; //
HashEntry* d_hash; //hash that stores pointers to sdf blocks
HashEntry* d_hashCompactified; //same as before except that only valid pointers are there
int* d_hashCompactifiedCounter; //atomic counter to add compactified entries atomically
Voxel* d_SDFBlocks; //sub-blocks that contain 8x8x8 voxels (linearized); are allocated by heap
int* d_hashBucketMutex;

}

Could someone tell me how I can solve the misaligned address erroe in the above two progam?
thank you very much!

In your first example, the misaligned address error is coming from a previous kernel call. This is due to CUDA asynchronous error reporting. You need to provide a short, complete example.

Alternatively, do rigorous error checking around your kernels, and use cudaDeviceSynchronize() after each kernel call (for debugging purposes only) to localize the error to the kernel that caused it.

Alternatively, learn to use cuda-memcheck

All of these suggestions are good practice for you to do before asking others for help.

1 Like