Skip to content

Commit

Permalink
libpressio version 0.6.0
Browse files Browse the repository at this point in the history
Major Features
+ Implemented a set of helper functions for POSIX compatible IO and
  accompanying set of tests.

Minor Features
+ New pressio_data_num_elements to get the number of elements in a
  pressio_data buffer

Bug Fixes
+ previous pressio_data_has_data returned the opposite value of what it
  should have
  • Loading branch information
robertu94 committed Aug 5, 2019
1 parent 5cd472b commit 903057e
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 25 deletions.
53 changes: 31 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(libpressio VERSION "0.5.0" LANGUAGES CXX C)
project(libpressio VERSION "0.6.0" LANGUAGES CXX C)

enable_testing()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand All @@ -22,37 +22,46 @@ configure_file(
)

add_library(libpressio
#public headers
./include/pressio_data.h
./include/pressio_compressor.h
./include/pressio.h
./include/pressio_options.h
./include/pressio_dtype.h
./include/pressio_option.h
./include/libpressio_ext/cpp/compressor.h

#implementations
#core implementation
./src/pressio.cc
./src/pressio_compressor.cc
./src/pressio_data.cc
./src/pressio_dtype.cc
./src/pressio_metrics.cc
./src/pressio_options.cc
./src/pressio_option.cc
./src/pressio_options.cc
./src/pressio_options_iter.cc

#plugins
./src/plugins/compressors/compressor_base.cc
./src/plugins/compressors/sz_plugin.cc
./src/plugins/compressors/zfp_plugin.cc
./src/plugins/metrics/composite.cc
./src/plugins/metrics/metrics_base.cc
./src/plugins/metrics/time.cc
./src/plugins/metrics/size.cc
./src/plugins/metrics/composite.cc
./src/pressio_data.cc
./src/pressio_dtype.cc
./src/pressio.cc
./src/pressio_compressor.cc
./src/pressio_options_iter.cc
./src/plugins/metrics/time.cc
./src/plugins/io/posix.cc

#public headers
include/libpressio.h
include/libpressio_ext/compressors/sz.h
include/libpressio_ext/compressors/zfp.h
include/libpressio_ext/cpp/compressor.h
include/libpressio_ext/cpp/metrics.h
include/libpressio_ext/cpp/plugins.h
include/libpressio_ext/io/posix.h
include/pressio.h
include/pressio_compressor.h
include/pressio_data.h
include/pressio_dtype.h
include/pressio_metrics.h
include/pressio_option.h
include/pressio_options.h
include/pressio_options_iter.h

#private headers
./src/plugins.h
./src/pressio_compressor_impl.h
./src/pressio_options_impl.h
src/pressio_compressor_impl.h
src/pressio_options_impl.h
)

target_include_directories(libpressio
Expand Down
93 changes: 93 additions & 0 deletions include/libpressio_ext/io/posix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

struct pressio_data;

/** \file
* \brief IO functions for POSIX compatible systems
*
* NOTE attempting to read files written by different machines has undefined behavior!!
*/

#ifndef PRESSIO_POSIX_IO
#define PRESSIO_POSIX_IO

/** read in a file from a POSIX FILE pointer
*
* NOTE attempting to read files written by different machines has undefined behavior!!
*
* \param[in,out] dims description of the dimension of the data if it known or NULL.
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents.
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(dims) returns true.
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in.
* \returns a pointer to a (possibly new) pressio data structure.
*
*/
struct pressio_data* pressio_io_data_fread(struct pressio_data* dims, FILE* in_file);


/** read in a file from a POSIX file descriptor
*
* NOTE attempting to read files written by different machines has undefined behavior!!
*
* \param[in,out] dims description of the dimension of the data if it known or NULL.
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents.
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(ptr) returns true.
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in. If dims is not null, only pressio_data_get_bytes(dims) bytes are read.
* \returns a pointer to a (possibly new) pressio data structure.
*
*/
struct pressio_data* pressio_io_data_read(struct pressio_data* dims, int in_filedes);

/** read in a file at a specifed location on the file-system
*
* NOTE attempting to read files written by different machines has undefined behavior!!
*
* \param[in,out] dims description of the dimension of the data if it known or NULL.
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents.
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(dims) returns true.
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in.
* \returns a pointer to a (possibly new) pressio data structure.
*
*/
struct pressio_data* pressio_io_data_path_read(struct pressio_data* dims, const char* out_file);

/** write in a file to the specified POSIX FILE pointer
*
* \param[in] data the data to be written.
* \param[in,out] out_file
* \returns the number of bytes written
*
*/
size_t pressio_io_data_fwrite(struct pressio_data* data, FILE* out_file);

/** write in a file to the specified POSIX file descriptor
*
* \param[in] data the data to be written.
* \param[in,out] out_filedes the file descriptor to write to
* \returns the number of bytes written
*
*/
size_t pressio_io_data_write(struct pressio_data* data, int out_filedes);

/** write in a file to the specified path on the file-system
*
* \param[in] data the data to be written.
* \param[in,out] path the path to write to
* \returns the number of bytes written
*
*/
size_t pressio_io_data_path_write(struct pressio_data* data, const char* path);


#endif /*PRESSIO_POSIX_IO*/

#ifdef __cplusplus
}
#endif
7 changes: 7 additions & 0 deletions include/pressio_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ size_t pressio_data_get_dimention(struct pressio_data const* data, size_t const
*/
size_t pressio_data_get_bytes(struct pressio_data const* data);

/**
* returns the total number of elements to represent the data
* \param[in] data the pressio data to query
* \returns the total number of elements to represent the data
*/
size_t pressio_data_num_elements(struct pressio_data const* data);

#endif

#ifdef __cplusplus
Expand Down
90 changes: 90 additions & 0 deletions src/plugins/io/posix.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include "pressio_data.h"
#include "libpressio_ext/io/posix.h"

namespace {
std::vector<size_t> get_all_dimentions(struct pressio_data const* data) {
std::vector<size_t> dims;
if(data) {
for (size_t i = 0; i < pressio_data_num_dimentions(data); ++i) {
dims.emplace_back(pressio_data_get_dimention(data, i));
}
}
return dims;
}
}

extern "C" {

struct pressio_data* pressio_io_data_read(struct pressio_data* dims, int in_filedes) {
pressio_data* ret;
if(dims != nullptr) {
if(pressio_data_has_data(dims)) {
//re-use the buffer provided by dims
ret = dims;
} else {
//create a new buffer of the appropriate size
auto dtype = pressio_data_dtype(dims);
auto dims_v = get_all_dimentions(dims);
pressio_data_free(dims);
ret = pressio_data_new_owning(dtype, dims_v.size(), dims_v.data());
}
} else {
struct stat statbuf;
if(fstat(in_filedes, &statbuf)) {
return nullptr;
}
size_t size = static_cast<size_t>(statbuf.st_size);
ret = pressio_data_new_owning(pressio_byte_dtype, 1, &size);
}
read(in_filedes, pressio_data_ptr(ret, nullptr), pressio_data_get_bytes(ret));
return ret;
}

struct pressio_data* pressio_io_data_fread(struct pressio_data* dims, FILE* in_file) {
return pressio_io_data_read(dims, fileno(in_file));
}


struct pressio_data* pressio_io_data_path_read(struct pressio_data* dims, const char* path) {
FILE* in_file = fopen(path, "r");
if(in_file != nullptr) {
auto ret = pressio_io_data_fread(dims, in_file);
fclose(in_file);
return ret;
} else {
return nullptr;
}
}


size_t pressio_io_data_fwrite(struct pressio_data* data, FILE* out_file) {

return fwrite(pressio_data_ptr(data, nullptr),
pressio_dtype_size(pressio_data_dtype(data)),
pressio_data_num_elements(data),
out_file
);
}

size_t pressio_io_data_write(struct pressio_data* data, int out_filedes) {
return write(out_filedes,
pressio_data_ptr(data, nullptr),
pressio_data_get_bytes(data)
);
}

size_t pressio_io_path_path_write(struct pressio_data* data, const char* path) {
FILE* out_file = fopen(path, "w");
if(out_file != nullptr) {
auto ret = pressio_io_data_fwrite(data, out_file);
fclose(out_file);
return ret;
} else {
return 0;
}
}

}
19 changes: 16 additions & 3 deletions src/pressio_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ void pressio_data_libc_free_fn(void* data, void*) {

namespace {
template <class T>
size_t data_size_in_bytes(pressio_dtype type, size_t dimentions, T const dims[]) {
size_t data_size_in_elements(size_t dimentions, T const dims[]) {
size_t totalsize = 1;
for (size_t i = 0; i < dimentions; ++i) {
totalsize *= dims[i];
}
return totalsize * pressio_dtype_size(type);
return totalsize;
}
template <class T>
size_t data_size_in_bytes(pressio_dtype type, size_t dimentions, T const dims[]) {
return data_size_in_elements(dimentions, dims) * pressio_dtype_size(type);
}
}

Expand Down Expand Up @@ -89,6 +93,10 @@ struct pressio_data {
return data_size_in_bytes(data_dtype, dimentions(), dims.data());
}

size_t num_elements() const {
return data_size_in_elements(dimentions(), dims.data());
}

private:
pressio_data(const pressio_dtype dtype,
void* data,
Expand Down Expand Up @@ -158,7 +166,7 @@ pressio_dtype pressio_data_dtype(struct pressio_data const* data) {
}

bool pressio_data_has_data(struct pressio_data const* data) {
return data->data() == nullptr;
return data->data() != nullptr;
}

size_t pressio_data_num_dimentions(struct pressio_data const* data) {
Expand All @@ -173,4 +181,9 @@ size_t pressio_data_get_bytes(struct pressio_data const* data) {
return data->size_in_bytes();
}

size_t pressio_data_num_elements(struct pressio_data const* data) {
return data->num_elements();
}


}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ endfunction()
add_gtest(test_pressio_data.cc)
add_gtest(test_pressio_options.cc)
add_gtest(test_sz_plugin.cc)
add_gtest(test_io.cc)
target_include_directories(test_sz_plugin PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../include)

add_executable(sz_basic sz_basic.c make_input_data.cc)
Expand Down
Loading

0 comments on commit 903057e

Please sign in to comment.