Skip to content
Snippets Groups Projects
Commit 4ea14661 authored by Pedro Gonnet's avatar Pedro Gonnet
Browse files

first stab at an asynchronous memory-mapped dumping mechanism. the dump...

first stab at an asynchronous memory-mapped dumping mechanism. the dump compiles, but cannot test yet as the test relies on a threadpool.
parent a4f97e08
Branches
Tags
1 merge request!297Streaming io
......@@ -36,14 +36,15 @@ endif
include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \
engine.h swift.h serial_io.h timers.h debug.h scheduler.h proxy.h parallel_io.h \
common_io.h single_io.h multipole.h map.h tools.h partition.h clocks.h parser.h \
physical_constants.h physical_constants_cgs.h potentials.h version.h hydro_properties.h
physical_constants.h physical_constants_cgs.h potentials.h version.h hydro_properties.h \
dump.h
# Common source files
AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \
serial_io.c timers.c debug.c scheduler.c proxy.c parallel_io.c \
units.c common_io.c single_io.c multipole.c version.c map.c \
kernel_hydro.c kernel_gravity.c tools.c part.c partition.c clocks.c parser.c \
physical_constants.c potentials.c hydro_properties.c
physical_constants.c potentials.c hydro_properties.c dump.c
# Include files for distribution, not installation.
nobase_noinst_HEADERS = approx_math.h atomic.h cycle.h error.h inline.h kernel_hydro.h kernel_gravity.h \
......
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2016 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
/* Config parameters. */
#include "../config.h"
/* Some standard headers. */
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* This object's header. */
#include "dump.h"
/* Local headers. */
#include "atomic.h"
#include "error.h"
/**
* @brief Obtain a chunk of memory from a dump.
*
* @param d The #dump.
* @param count The number of bytes requested.
* @param offset The offset of the returned memory address within the dump file.
* @return A pointer to the memory-mapped chunk of data.
*/
void *dump_get(struct dump *d, size_t count, size_t *offset) {
size_t local_offset = atomic_add(&d->count, count);
*offset = local_offset + d->file_offset;
return d->data + local_offset;
}
/**
* @brief Ensure that at least size bytes are available in the #dump.
*/
void dump_ensure(struct dump *d, size_t size) {
/* If we have enough space already, just bail. */
if (d->size - d->count > size) return;
/* Unmap the current data. */
if (munmap(d->data, d->count) != 0) error("Failed to unmap dump data.");
/* Re-map starting at the end of the file. */
d->file_offset += d->count;
d->size = size * dump_grow_ensure_factor;
if ((d->data = mmap(NULL, d->size, PROT_WRITE, MAP_PRIVATE, d->fd,
d->file_offset)) == MAP_FAILED) {
error("Failed to allocate map of size %zi bytes.", size);
}
/* Re-set the counter. */
d->count = 0;
}
/**
* @brief Flush the #dump to disk.
*/
void dump_sync(struct dump *d) {
if (msync(d->data, d->count, MS_SYNC) != 0)
error("Failed to sync memory-mapped data.");
}
/**
* @brief Finalize the #dump.
*/
void dump_close(struct dump *d) {
/* Unmap the data in memory. */
if (munmap(d->data, d->count) != 0) error("Failed to unmap dump data.");
/* Close the memory-mapped file. */
if (close(d->fd) != 0) error("Failed to close memory-mapped file.");
}
/**
* @brief Initialize a file dump.
*
* @param d The #dump to initialize.
* @param filename The fully qualified name of the file in which to dump,
* note that it will be overwritten.
* @param size The initial buffer size for this #dump.
*/
void dump_init(struct dump *d, const char *filename, size_t size) {
/* Create the output file. */
if ((d->fd = open(filename, O_WRONLY)) == -1) {
error("Failed to create dump file '%s' (%s).", filename, strerror(errno));
}
/* Map memory to the created file. */
if ((d->data = mmap(NULL, size, PROT_WRITE, MAP_PRIVATE, d->fd, 0)) ==
MAP_FAILED) {
error("Failed to allocate map of size %zi bytes.", size);
}
/* Init some counters. */
d->size = size;
d->count = 0;
d->file_offset = 0;
}
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2016 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#ifndef SWIFT_DUMP_H
#define SWIFT_DUMP_H
/* Includes. */
#include "cell.h"
#include "lock.h"
#include "task.h"
/* Some constants. */
#define dump_grow_ensure_factor 10
/** The dump struct. */
struct dump {
/* The memory-mapped data of this dump. */
void *data;
/* The size of the memory-mapped data, in bytes. */
size_t size;
/* The number of bytes that have been dumped. */
size_t count;
/* The offset of the data within the current file. */
size_t file_offset;
/* The file with which this memory is associated. */
int fd;
};
/* Function prototypes. */
void dump_init(struct dump *d, const char *filename, size_t size);
void dump_ensure(struct dump *d, size_t size);
void dump_sync(struct dump *d);
void dump_close(struct dump *d);
void *dump_get(struct dump *d, size_t count, size_t *offset);
#endif /* SWIFT_DUMP_H */
......@@ -22,11 +22,11 @@ AM_LDFLAGS = ../src/.libs/libswiftsim.a $(HDF5_LDFLAGS) $(HDF5_LIBS)
# List of programs and scripts to run in the test suite
TESTS = testGreetings testReading.sh testSingle testPair.sh testPairPerturbed.sh \
test27cells.sh test27cellsPerturbed.sh testParser.sh testKernel
test27cells.sh test27cellsPerturbed.sh testParser.sh testKernel dump_test
# List of test programs to compile
check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \
testSPHStep testPair test27cells testParser testKernel
testSPHStep testPair test27cells testParser testKernel dump_test
# Sources for the individual programs
testGreetings_SOURCES = testGreetings.c
......@@ -47,6 +47,8 @@ testParser_SOURCES = testParser.c
testKernel_SOURCES = testKernel.c
dump_test_SOURCES = dump_test.c
# Files necessary for distribution
EXTRA_DIST = testReading.sh makeInput.py testPair.sh testPairPerturbed.sh \
test27cells.sh test27cellsPerturbed.sh tolerance.dat testParser.sh \
......
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2016 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
/* Config parameters. */
#include "../config.h"
/* Some standard headers. */
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* This object's header. */
#include "dump.h"
/* Local headers. */
#include "threadpool.h"
void dump_mapper(void *map_data, int num_elements, void *extra_data) {
struct dump *d = (struct dump *)extra_data;
int offset;
char *out_string = dump_get(d, 7, &offset);
snprintf(7, "%06i\n", offset);
}
int main(int argc, char *argv[]) {
/* Some constants. */
const int num_threads = 4;
const char *filename = "/tmp/dump_test.out";
const int num_runs = 20;
const int chunk_size = 1000;
/* Prepare a threadpool to write to the dump. */
struct threadpool t;
threadpool_init(&t, num_threads);
/* Prepare a dump. */
struct dump d;
dump_init(&d, filename, 1024);
/* Dump numbers in chunks. */
for (int run = 0; runs < num_runs; runs++) {
/* Ensure capacity. */
dump_ensure(&d, 7 * chunk_size);
/* Dump a few numbers. */
threadpool_map(&t, dump_mapper, NULL, chunk_size, 0, 1, &d);
}
/* Finalize the dump. */
dump_close(&d);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment