Skip to content

Commit

Permalink
added type int64 wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
halehawk committed May 15, 2019
1 parent d803a45 commit b867c7c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.9)
project(cz5test C CXX)

message(STATUS "Using C11")
message(CXX)
set(CMAKE_C_STANDARD 11)

if (NOT "${CMAKE_CXX_FLAGS}" MATCHES "-std=c\\+\\+17")
Expand Down Expand Up @@ -29,6 +30,7 @@ option(WITH_ZLIB ON)
if(WITH_ZLIB)
find_package(ZLIB REQUIRED)
include_directories(ZLIB_INCLUDE_DIRS)
message(ZLIB_INCLUDE_DIRS)
add_definitions(-DWITH_ZLIB)
SET(COMPRESSION_LIBRARIES "${COMPRESSION_LIBRARIES};${ZLIB_LIBRARIES}")
endif()
Expand Down
30 changes: 30 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ int main() {
float data1[10][10][10];
float data2[10][10][10];
float rdata[10][10][10];
long long int idata1[10][10][10];
long long int idata2[10][10][10];
long long int irdata[10][10][10];
unsigned int ndim = 3;
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++){
for (int k = 0; k<chunks[2]; k++){
data1[i][j][k]=rand()%1000+1;
idata1[i][j][k]=rand()%1000+1;
}
}
}
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++){
for (int k = 0; k<chunks[2]; k++){
data2[i][j][k]=rand()%1000+1;
idata2[i][j][k]=rand()%1000+1;
}
}
}
Expand All @@ -48,5 +53,30 @@ int main() {
printf("data2 = %f\n",data2[i][0][0]);
}
printf("after assert\n");

char* iarrayName = "test_c_int64.z5";
z5CreateInt64Dataset(iarrayName, ndim, shape, chunks, cusezlib, level);
z5WriteInt64Subarray(iarrayName, idata1, ndim, chunks, offset);
z5ReadInt64Subarray(iarrayName, irdata, ndim, chunks, offset);
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++)
for (int k = 0; k<chunks[2]; k++)
assert(idata1[i][j][k] == irdata[i][j][k]);
printf("idata1 = %lu\n",idata1[i][0][0]);
}
z5WriteInt64Subarray(iarrayName, idata2, ndim, chunks, offset2);
z5ReadInt64Subarray(iarrayName, irdata, ndim, chunks, offset2);
printf("after read float\n");
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++)
for (int k = 0; k<chunks[2]; k++)
assert(idata2[i][j][k] == irdata[i][j][k]);
printf("idata2 = %lu\n",idata2[i][0][0]);
}
printf("after assert\n");
size_t chunkSize0=z5GetFileSize("test_c_int64.z5/0.0.0");
size_t chunkSize1=z5GetFileSize("test_c_int64.z5/1.1.1");
printf("size = %lu, %lu\n",chunkSize0,chunkSize1);
z5Delete("test_c_int64.z5");
return 0;
}
69 changes: 69 additions & 0 deletions z5wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iostream>
#include "z5wrapper.h"

namespace fs = boost::filesystem;
namespace z5 {
extern "C" {

Expand Down Expand Up @@ -34,6 +35,30 @@ namespace z5 {

}

void z5CreateInt64Dataset(char *path, unsigned int ndim, size_t *shape, size_t *chunks, int cuseZlib, int level) {
std::string path_s(path);
std::vector<std::string> dtype({"long long int"});

std::vector<size_t> shape_v(shape, shape + ndim);
std::vector<size_t> chunks_v(chunks, chunks + ndim);
bool asZarr = true;

DatasetMetadata int64Meta(types::Datatype::int64,shape_v,chunks_v,asZarr);
if (cuseZlib) {
int64Meta.compressor = types::zlib;
int64Meta.compressionOptions["useZlib"] = true;
int64Meta.compressionOptions["level"] = level;
}
handle::Dataset handle_(path_s);
handle_.createDir();
writeMetadata(handle_, int64Meta);
long long int idata1;
int64_t data1;
long int data2;
//std::cout<<"int64 "<<typeid(data1).name()<<" "<<sizeof(int64_t)<<std::endl;
//std::cout<<"long int "<<typeid(data2).name()<<" "<<sizeof(long int)<<std::endl;
//std::cout<<"long long int "<<typeid(idata1).name()<<" "<<sizeof(long long int)<<std::endl;
}

void z5WriteFloatSubarray(char *path, float *array, unsigned int ndim, size_t *shape, size_t *offset) {
std::string path_s(path);
Expand All @@ -47,6 +72,21 @@ namespace z5 {
multiarray::writeSubarray<float>(ds,adp_array,offset_v.begin());
}

void z5WriteInt64Subarray(char *path, long long int *array, unsigned int ndim, size_t *shape, size_t *offset) {
std::string path_s(path);
auto ds =openDataset(path_s);
using vec_type = std::vector<long int>;
size_t size = 1;
std::vector<std::size_t> shape_v(shape,shape + ndim);
for (std::vector<size_t>::const_iterator i = shape_v.begin(); i != shape_v.end(); ++i)
size=size*(*i);
using shape_type = std::vector<vec_type::size_type>;
shape_type s(shape,shape+ndim);
std::vector<size_t> offset_v(offset,offset + ndim);
auto adp_array=xt::adapt(array,size,xt::no_ownership(),s);
multiarray::writeSubarray<long int>(ds,adp_array,offset_v.begin());
}

void z5ReadFloatSubarray(char *path, float *array, unsigned int ndim, size_t *shape, size_t *offset) {
std::string path_s(path);
auto ds = openDataset(path_s);
Expand All @@ -61,6 +101,35 @@ namespace z5 {
auto adp_array=xt::adapt(array,size,xt::no_ownership(),s);
multiarray::readSubarray<float>(ds,adp_array,offset_v.begin());
}
void z5ReadInt64Subarray(char *path, long long int *array, unsigned int ndim, size_t *shape, size_t *offset) {
std::string path_s(path);
auto ds = openDataset(path_s);
using vec_type = std::vector<long int>;
size_t size = 1;
std::vector<std::size_t> shape_v(shape,shape + ndim);
for (std::vector<size_t>::const_iterator i = shape_v.begin(); i != shape_v.end(); ++i)
size*=(*i);
using shape_type = std::vector<vec_type::size_type>;
shape_type s(shape,shape+ndim);
std::vector<size_t> offset_v(offset,offset + ndim);
auto adp_array=xt::adapt(array,size,xt::no_ownership(),s);
multiarray::readSubarray<long int>(ds,adp_array,offset_v.begin());
}
size_t z5GetFileSize(char *path){
std::string path_s(path);
fs::ifstream file(path_s, std::ios::binary);
file.seekg(0, std::ios::end);
const std::size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
file.close();
return fileSize;
}
void z5Delete(char *path ){
std::string path_s(path);
fs::path filename(path_s);
fs::remove_all(filename);
}

}

}
10 changes: 10 additions & 0 deletions z5wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ namespace z5 {
#endif

void z5CreateGroup(char* path);

void z5CreateFloatDataset(char *path, unsigned int ndim, size_t *shape, size_t *chunks, int cuseZlib, int level);

void z5WriteFloatSubarray(char *path, float *array, unsigned int ndim, size_t *shape, size_t *offset);

void z5ReadFloatSubarray(char *path, float *array, unsigned int ndim, size_t *shape, size_t *offset);

void z5CreateInt64Dataset(char *path, unsigned int ndim, size_t *shape, size_t *chunks, int cuseZlib, int level);

void z5WriteInt64Subarray(char *path, long long int *array, unsigned int ndim, size_t *shape, size_t *offset);

void z5ReadInt64Subarray(char *path, long long int *array, unsigned int ndim, size_t *shape, size_t *offset);

size_t z5GetFileSize(char *path);

void z5Delete(char *path );
#ifdef __cplusplus
}
}
Expand Down

0 comments on commit b867c7c

Please sign in to comment.