jacobian

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 384b86c4270d0f4d32242d90a530919f5c17218e
parent e802136589583b2f9e3766a46f85b3a94f496ef4
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Fri, 25 Sep 2020 16:59:13 -0700

Incorporating faster file read into BPNN

Diffstat:
Mexample.cpp | 3++-
Msrc/bpnn.cpp | 7++++---
Msrc/data.cpp | 99++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 99 insertions(+), 10 deletions(-)

diff --git a/example.cpp b/example.cpp @@ -19,7 +19,7 @@ double bench(int batch_sz, int epochs) { auto start = std::chrono::high_resolution_clock::now(); - Network net ("./data_banknote_authentication.txt", batch_sz, 0.0155, 0.03, L2, 0, 0.9); + Network net ("./data_banknote_authentication.bak.bin", batch_sz, 0.0155, 0.03, L2, 0, 0.9); net.add_layer(4, "linear", linear, linear_deriv); net.add_layer(5, "lecun_tanh", lecun_tanh, lecun_tanh_deriv); net.add_layer(2, "linear", linear, linear_deriv); @@ -34,6 +34,7 @@ double bench(int batch_sz, int epochs) int main(int argc, char** argv) { if (argc < 2) { + prep(); std::cout << "Invalid command! Either pass a special option or pass two integers - batch_size and epochs (in that order)." << "\n"; exit(1); } diff --git a/src/bpnn.cpp b/src/bpnn.cpp @@ -74,8 +74,8 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate, Re { int total_instances = prep_file(path, SHUFFLED_PATH); val_instances = split_file(SHUFFLED_PATH, total_instances, ratio); - data = open(TRAIN_PATH, O_RDONLY & O_NONBLOCK); - val_data = open(VAL_PATH, O_RDONLY & O_NONBLOCK); + data = open(TRAIN_PATH, O_RDONLY | O_NONBLOCK); + val_data = open(VAL_PATH, O_RDONLY | O_NONBLOCK); instances = total_instances - val_instances; assert(batch_size > 0 || batch_size < instances); decay = [this]() -> void {}; @@ -84,6 +84,7 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate, Re }; } + void Network::init_decay(char* type, ...) { va_list args; @@ -368,7 +369,7 @@ void Network::train() validate(VAL_PATH); if (silenced == false) printf("Epoch %i complete - cost %f - acc %f - val_cost %f - val_acc %f\n", epochs, epoch_cost, epoch_acc, val_cost, val_acc); batches=1; - data = open(TRAIN_PATH, O_RDONLY & O_NONBLOCK); + data = open(TRAIN_PATH, O_RDONLY | O_NONBLOCK); decay(); epochs++; } diff --git a/src/data.cpp b/src/data.cpp @@ -12,18 +12,105 @@ inline float scan(char **p) return n*neg; } -int Network::next_batch() +void prep() { - uintmax_t lines = 0; - while(size_t bytes_read = read(data, buf, BUFFER_SIZE)) + FILE* wptr = fopen("../../data_banknote_authentication.bin", "wb"); + static const auto BUFFER_SIZE = 512*1024; + int fd = open("../../data_banknote_authentication.txt", O_RDONLY | O_NONBLOCK); + if(fd == -1) { + printf("fd == -1\n"); + } + float tmp; + char buf[BUFFER_SIZE + 1]; + while(size_t bytes_read = read(fd, buf, BUFFER_SIZE)) { + if(bytes_read == (size_t)-1) { + printf("bytes_read == (size_t)-1\n"); + } if (!bytes_read) break; - for(char *p = buf; lines < 10;) { + for(char *p = buf;;) { char* bound = (char*) memchr(p, '\n', (buf + bytes_read) - p); if (bound - p < 0) break; // Stop. - for (int i=0; i<layers[0].contents->cols(); ++i) (*layers[0].contents)(lines,i) = scan(&p); - (*labels)(lines,0) = scan(&p); + for (int i=0; i<5; ++i) { + tmp = scan(&p); + fwrite((void*)&tmp, sizeof(float), 1, wptr); + } p = bound + 1; + } + } +} + +float mmap_read() +{ + static const auto BUFFER_SIZE = 600*1024; + int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK); + if(fd == -1) { + printf("Cannot open file."); + exit(1); + } + int rc, ii; + struct stat st; + size_t size; + rc = fstat(fd, &st); + size=st.st_size; + float* ptr = (float*) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); + madvise(ptr, size, POSIX_MADV_SEQUENTIAL); + char buf[BUFFER_SIZE]; + uintmax_t lines = 0; + float tmp; + for (ii=0; ii < size/sizeof *ptr; ii++) { + // Nothin. + } + rc = munmap(ptr, size); + close(fd); + return tmp; +} + +float vec_read() +{ +#define CHUNK_SZ (200*1024) +#define BUFFER_SZ (600*1024) +#define NUM_CHUNKS (BUFFER_SZ/CHUNK_SZ) + int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK); + char rawbuf[BUFFER_SZ]; + char* buf = (char*) rawbuf; + iovec iovecs[NUM_CHUNKS]; + for (int i = 0; i < BUFFER_SZ; i+=CHUNK_SZ) { + iovecs[i/CHUNK_SZ].iov_base = buf + i; + iovecs[i/CHUNK_SZ].iov_len = CHUNK_SZ; + } + while(size_t bytes_read = readv(fd, iovecs, NUM_CHUNKS)) + { + if(bytes_read == (size_t)-1) { + printf("\n%zu\n", bytes_read); + printf("bytes_read == (size_t)-1\n"); + exit(1); + } + if (!bytes_read) break; + } + return 0; +} + +float std_read(int BUFFER_SIZE) +{ + int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK); + fcntl(fd, F_RDADVISE); + if(fd == -1) { + + +int Network::next_batch() +{ + uintmax_t lines = 0; + while(size_t bytes_read = read(data, buf, BUFFER_SIZE)) + { + if (!bytes_read) break; + for(char *p = buf; p < buf+BUFFER_SIZE && lines < 10;) { + for (int i=0; i<<layers[0].contents->cols(); ++i) { + (*layers[0].contents)(lines,i) = *((float*)p); + p += sizeof(float); + } + (*labels)(lines,0) = *((float*)p); + p += sizeof(float); ++lines; } }