Introduction
When using StarPU, one may need to store more data than what the main memory (RAM) can store. This part describes the method to add a new memory node on a disk and to use it.
The principle is that one first registers a disk location, seen by StarPU as a void*, which can be for instance a Unix path for the stdio or unistd case, or a database file path for a leveldb case, etc. The disk backend opens this place with the plug method.
If the disk backend provides an alloc method, StarPU can then start using it to allocate room and store data there with the write method, without user intervention.
The user can also use starpu_disk_open to explicitly open an object within the disk, e.g. a file name in the stdio or unistd cases, or a database key in the leveldb case, and then use starpu_*_register functions to turn it into a StarPU data handle. StarPU will then automatically read and write data as appropriate.
Use a new disk memory
To use a disk memory node, you have to register it with this function:
Here, we use the unistd library to realize the read/write operations, i.e. fread/fwrite. This structure must have a path where to store files, as well as the maximum size the software can afford storing on the disk.
Don't forget to check if the result is correct!
This can also be achieved by just setting environment variables:
export STARPU_DISK_SWAP=/tmp
export STARPU_DISK_SWAP_BACKEND=unistd
export STARPU_DISK_SWAP_SIZE=$((200*1024*1024))
When the register function is called, StarPU will benchmark the disk. This can take some time.
Warning: the size thus has to be at least 1 MB!
StarPU will automatically try to evict unused data to this new disk. One can also use the standard StarPU memory node API, see the Standard Memory Library and the Data Interfaces .
The disk is unregistered during the starpu_shutdown().
Disk functions
There are various ways to operate a disk memory node, described by the structure starpu_disk_ops. For instance, the variable starpu_disk_unistd_ops uses read/write functions.
All structures are in Out Of Core .
Examples: disk_copy
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define NX (30*1000000/sizeof(double))
#define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
int main(int argc, char **argv)
{
double * A,*B,*C,*D,*E,*F;
setenv("STARPU_LIMIT_CPU_MEM", "160", 1);
if (ret == -ENODEV) goto enodev;
if (new_dd == -ENOENT) goto enoent;
FPRINTF(stderr, "TEST DISK MEMORY \n");
unsigned int j;
for(j = 0; j < NX; ++j)
{
A[j] = j;
F[j] = -j;
}
starpu_data_handle_t vector_handleA, vector_handleB, vector_handleC, vector_handleD, vector_handleE, vector_handleF;
int try = 1;
for (j = 0; j < NX; ++j)
if (A[j] != F[j])
{
printf("Fail A %f != F %f \n", A[j], F[j]);
try = 0;
}
if(try)
FPRINTF(stderr, "TEST SUCCESS\n");
else
FPRINTF(stderr, "TEST FAIL\n");
return (try ? EXIT_SUCCESS : EXIT_FAILURE);
enodev:
return 77;
enoent:
return 77;
}
Examples: disk_compute
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#define NX (1024)
int main(int argc, char **argv)
{
if (ret == -ENODEV) goto enodev;
char pid_str[16];
int pid = getpid();
snprintf(pid_str, 16, "%d", pid);
const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
strcpy(path_file_start, base);
strcat(path_file_start, "/");
strcat(path_file_start, name_file_start);
char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
strcpy(path_file_end, base);
strcat(path_file_end, "/");
strcat(path_file_end, name_file_end);
if (new_dd == -ENOENT) goto enoent;
unsigned dd = (unsigned) new_dd;
printf("TEST DISK MEMORY \n");
int *A;
int *C;
unsigned int j;
for(j = 0; j < NX; ++j)
{
A[j] = j;
C[j] = 0;
}
FILE * f = fopen(path_file_start, "wb+");
if (f == NULL)
goto enoent2;
fwrite(A, sizeof(int), NX, f);
fclose(f);
f = fopen(path_file_end, "wb+");
if (f == NULL)
goto enoent2;
fwrite(C, sizeof(int), NX, f);
fclose(f);
void * data_result =
starpu_disk_open(dd, (
void *) name_file_end, NX*
sizeof(
int));
f = fopen(path_file_end, "rb+");
if (f == NULL)
goto enoent;
int size = fread(C, sizeof(int), NX, f);
fclose(f);
int try = 1;
for (j = 0; j < NX; ++j)
if (A[j] != C[j])
{
printf("Fail A %d != C %d \n", A[j], C[j]);
try = 0;
}
unlink(path_file_start);
unlink(path_file_end);
free(path_file_start);
free(path_file_end);
if(try)
printf("TEST SUCCESS\n");
else
printf("TEST FAIL\n");
return (try ? EXIT_SUCCESS : EXIT_FAILURE);
enodev:
return 77;
enoent2:
enoent:
unlink(path_file_start);
unlink(path_file_end);
free(path_file_start);
free(path_file_end);
return 77;
}