Re: need fastest way to write 2gig array to disk file
From: Basile Starynkevitch [news] (basile-news_at_starynkevitch.net)
Date: 09/10/05
- Next message: Moritz Beller: "home directory"
- Previous message: Kasper Dupont: "Re: why my java process may have vaporized?"
- In reply to: Eric Taylor: "need fastest way to write 2gig array to disk file"
- Next in thread: Eric Taylor: "Re: need fastest way to write 2gig array to disk file"
- Reply: Eric Taylor: "Re: need fastest way to write 2gig array to disk file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 10 Sep 2005 06:54:52 +0000 (UTC)
On 2005-09-09, Eric Taylor <et1@rocketship1.com> wrote:
> I have a two gigabtye array. From a nothing special C program
> what is the fastest way to write this to a disk file.
You might consider using mmap on this array. Basically, instead of
allocating the array with malloc, you open a file for writing, then
mmap it, then msync; something like (untested code, and you really
should add all the error checks)
// the array size in byte has to be a multiple of the page size
#define BIGSIZE 16384*1024 /* number of doubles in your array */
int fd = open("your_big_file", O_RDWR|O_CREAT, 0640);
if (fd<0) errored("failed to open");
size_t arraysize = sizeof(double)*BIGSIZE;
// grow the file if needed by truncating it
ftruncate(fd, arraysize);
// map the array in memory
void* arrayaddr = mmap((void*)0, arraysize, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, (off_t)0);
if (arrayaddr==MAP_FAILED) errored("failed to mmap");
double* array = arrayaddr;
// compute & fill the array
compute_your_big_array(array, BIGSIZE);
// sync the array to disk
msync(ad, arraysize, MS_ASYNC);
// unmap memory and close file
munmap(arrayaddr, arraysize);
close(fd);
Read a good book on Linux or Posix system programming, and the man pages for
open(2), mmap(2), msync(2), munmap(2), close(2) system calls
Regards
-- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basile(at)starynkevitch(dot)net 8, rue de la Faïencerie, 92340 Bourg La Reine, France
- Next message: Moritz Beller: "home directory"
- Previous message: Kasper Dupont: "Re: why my java process may have vaporized?"
- In reply to: Eric Taylor: "need fastest way to write 2gig array to disk file"
- Next in thread: Eric Taylor: "Re: need fastest way to write 2gig array to disk file"
- Reply: Eric Taylor: "Re: need fastest way to write 2gig array to disk file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|