Last active
October 29, 2015 15:57
-
-
Save allanmac/9263979 to your computer and use it in GitHub Desktop.
Allocate more than 4GB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// | |
// | |
// | |
static | |
void | |
cuda_assert(const cudaError_t code, const char* const file, const int line, const bool abort) | |
{ | |
if (code != cudaSuccess) | |
{ | |
fprintf(stderr,"cuda_assert: %s %s %d\n",cudaGetErrorString(code),file,line); | |
if (abort) | |
exit(code); | |
} | |
} | |
#define cuda(...) { cuda_assert((cuda##__VA_ARGS__), __FILE__, __LINE__, true); } | |
// | |
// | |
// | |
size_t meminfo() | |
{ | |
size_t free, total; | |
cuda(MemGetInfo(&free,&total)); | |
#if defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) | |
printf("free/total (MB): %7llu / %7llu\n", | |
free /(1024ll*1024ll), | |
total/(1024ll*1024ll)); | |
#else | |
printf("free/total (MB): %7u / %7u\n", | |
free /(1024*1024), | |
total/(1024*1024)); | |
#endif | |
return free; | |
} | |
// | |
// | |
// | |
int main(int argc, char** argv) | |
{ | |
int device = (argc == 2) ? atoi(argv[1]) : 0; | |
cudaDeviceProp props; | |
cuda(GetDeviceProperties(&props,device)); | |
printf("%s (%2d)\n",props.name,props.multiProcessorCount); | |
cuda(SetDevice(device)); | |
// | |
// | |
// | |
size_t free = meminfo(); | |
// | |
// | |
// | |
size_t allocation = free - (256*1024*1024); // allocate (free - 256MB) | |
#if defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) | |
printf("cudaMalloc (MB): %7llu\n",allocation/(1024ll*1024ll)); | |
#else | |
printf("cudaMalloc (MB): %7u\n", allocation/(1024*1024)); | |
#endif | |
// | |
// | |
// | |
void* mem; | |
cuda(Malloc(&mem,allocation)); | |
// | |
// | |
// | |
meminfo(); | |
// | |
// | |
// | |
cuda(Free(mem)); | |
cuda(DeviceReset()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added error checking... but also rebooted. Results are now as expected!
64-bit:
32-bit: