Here are my full code:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iostream>
class Managed {
public:
void *operator new(size_t len) {
void *ptr;
cudaMallocManaged(&ptr, len);
return ptr;
}
void operator delete(void *ptr) {
cudaFree(ptr);
}
};
class Encounter : public Managed {
public:
int ID; // 4 bytes
int Age; // 4 bytes
char Gender; // 1 byte
Encounter(const int id, const int age, const char gender) {
ID = id;
Age = age;
Gender = gender;
}
~Encounter() { }
};
__global__ void Launch(Encounter ** e) { }
int main()
{
const int caseCount = 100;
std::cout << sizeof(char) << std::endl;
cudaError_t cudaStatus = cudaSetDevice(0);
size_t mf, ma;
cudaMemGetInfo(&mf, &ma);
Encounter ** encounters;
cudaMallocManaged(&encounters, sizeof(Encounter**) * caseCount);
size_t lastFree = mf;
for (int i = 0; i < caseCount; i++) {
Encounter * e = new Encounter(i, 18, 'F');
encounters[i] = e;
cudaMemGetInfo(&mf, &ma);
std::cout << "Free:" << mf << " Total:" << ma << " Used:" << (lastFree - mf) << std::endl;
lastFree = mf;
}
Launch << <1, 1 >> > (encounters);
cudaDeviceSynchronize();
cudaDeviceReset();
return 0;
}
Let me explain:
I surpose that Encounter object size will be: 9 bytes (int + int + char), but due to alignment 512 for x64, lead to the real malloc size will be 512:
So 100 array length will be: 512 * 100 = 51,200
Plus cudaMallocManaged of array of encounter: 8 * 100 = 800 => real alloc memory = 1024
So total real malloc memory should be: 1024 + 51,200 = 52,224
But my output test:
Free:1723825766 Total:2147483648 Used:2097152
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1723825766 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:2097152
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1721728614 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:2097152
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1719631462 Total:2147483648 Used:0
Free:1717534310 Total:2147483648 Used:2097152
Free:1717534310 Total:2147483648 Used:0
Free:1717534310 Total:2147483648 Used:0
Free:1717534310 Total:2147483648 Used:0
Free:1717534310 Total:2147483648 Used:0
I knew that 2097152 is cache/page size, but due to this value does not over of 52,224, so it should be only one used register memory.
Could you please help me to find a calculation to get right value of my output test?
I tested on:
Windows 10 64-bit
Nvidia GeForce GTX 1050
Thank you so much!
VyPhan