]> git.donarmstrong.com Git - rsem.git/blobdiff - Buffer.h
roll back to original version plus fixed a small bug in calcCI.cpp
[rsem.git] / Buffer.h
index 1c5a2d6f5415a901a9ec7ee335207a2644d2d6d6..1ce28bca18d4e21193965886cd92e0d64e5a927e 100644 (file)
--- a/Buffer.h
+++ b/Buffer.h
@@ -3,36 +3,48 @@
 
 #include<cstdio>
 #include<fstream>
+#include<pthread.h>
+
+#include "my_assert.h"
 
 typedef unsigned long long bufsize_type;
 const int FLOATSIZE = sizeof(float);
 
 class Buffer {
 public:
-       Buffer(bufsize_type size, int sp, int nSamples, int cvlen, const char* tmpF) {
+       Buffer(int nMB, int nSamples, int cvlen, const char* tmpF) {
                cpos = 0;
-               this->size = size;
+               size = bufsize_type(nMB) * 1024 * 1024 / FLOATSIZE / cvlen;
+               if (size > (bufsize_type)nSamples) size = nSamples;
+               general_assert(size > 0, "Memory allocated for credibility intervals is not enough!");
+               size *= cvlen;
+
                buffer = new float[size];
                ftmpOut.open(tmpF, std::ios::binary);
+               pthread_mutex_init(&lock, NULL);
 
-               fr = to = sp;
+               fr = to = 0;
                this->nSamples = nSamples;
                this->cvlen = cvlen;
-
        }
 
        ~Buffer() {
                if (fr < to) flushToTempFile();
 
                delete[] buffer;
+               pthread_mutex_destroy(&lock);
                ftmpOut.close();
        }
 
-       void write(float *vec) {
-               if (size - cpos < bufsize_type(cvlen)) flushToTempFile();
-               memcpy(buffer + cpos, vec, FLOATSIZE * cvlen);
-               cpos += cvlen;
-               ++to;
+       void write(int n, float **vecs) {
+               pthread_assert(pthread_mutex_lock(&lock), "pthread_mutex_lock", "Error occurred while acquiring the lock!");
+               for (int i = 0; i < n; i++) {
+                       if (size - cpos < bufsize_type(cvlen)) flushToTempFile();
+                       memcpy(buffer + cpos, vecs[i], FLOATSIZE * cvlen);
+                       cpos += cvlen;
+                       ++to;
+               }
+               pthread_assert(pthread_mutex_unlock(&lock), "pthread_mutex_unlock", "Error occurred while releasing the lock!");
        }
 
 private:
@@ -40,6 +52,7 @@ private:
 
        float *buffer;
        std::ofstream ftmpOut;
+       pthread_mutex_t lock;
 
        int fr, to; // each flush, sample fr .. to - 1
        int nSamples, cvlen;
@@ -49,15 +62,15 @@ private:
                std::streampos gap2 = std::streampos(nSamples - to) * FLOATSIZE;
                float *p = NULL;
 
-               ftmpOut.seekp(0, std::ios::beg);
+               ftmpOut.seekp(0, std::ios_base::beg);
                for (int i = 0; i < cvlen; i++) {
                        p = buffer + i;
-                       ftmpOut.seekp(gap1, std::ios::cur);
+                       ftmpOut.seekp(gap1, std::ios_base::cur);
                        for (int j = fr; j < to; j++) {
                                ftmpOut.write((char*)p, FLOATSIZE);
                                p += cvlen;
                        }
-                       ftmpOut.seekp(gap2, std::ios::cur);
+                       ftmpOut.seekp(gap2, std::ios_base::cur);
                }
 
                cpos = 0;