X-Git-Url: https://git.donarmstrong.com/?p=rsem.git;a=blobdiff_plain;f=Buffer.h;h=3e450942509b8cfe52b544bbcc6533bd06c37e59;hp=50177960a94a2508b0df9a5c2c590502b23954f2;hb=910250881a4c3e48a33aa427b82131acda914da3;hpb=237bbdf363c9e42ee24e2fd63106dccf20d9bf2f diff --git a/Buffer.h b/Buffer.h index 5017796..3e45094 100644 --- a/Buffer.h +++ b/Buffer.h @@ -12,12 +12,13 @@ const int FLOATSIZE = sizeof(float); class Buffer { public: - Buffer(int nMB, int nSamples, int cvlen, const char* tmpF) { + // in_mem_arr must be allocated memory before the Buffer is constructed + Buffer(int nMB, int nSamples, int vlen, float* in_mem_arr, const char* tmpF) { cpos = 0; - size = bufsize_type(nMB) * 1024 * 1024 / FLOATSIZE / cvlen; + size = bufsize_type(nMB) * 1024 * 1024 / FLOATSIZE / vlen; if (size > (bufsize_type)nSamples) size = nSamples; general_assert(size > 0, "Memory allocated for credibility intervals is not enough!"); - size *= cvlen; + size *= vlen; buffer = new float[size]; ftmpOut.open(tmpF, std::ios::binary); @@ -25,7 +26,8 @@ public: fr = to = 0; this->nSamples = nSamples; - this->cvlen = cvlen; + this->vlen = vlen; + this->in_mem_arr = in_mem_arr; } ~Buffer() { @@ -36,14 +38,13 @@ public: ftmpOut.close(); } - void write(int n, float **vecs) { + void write(float value, float *vec) { 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; - } + if (size - cpos < bufsize_type(vlen)) flushToTempFile(); + in_mem_arr[to] = value; + memcpy(buffer + cpos, vec, FLOATSIZE * vlen); + cpos += vlen; + ++to; pthread_assert(pthread_mutex_unlock(&lock), "pthread_mutex_unlock", "Error occurred while releasing the lock!"); } @@ -51,11 +52,12 @@ private: bufsize_type size, cpos; // cpos : current position float *buffer; + float *in_mem_arr; std::ofstream ftmpOut; pthread_mutex_t lock; int fr, to; // each flush, sample fr .. to - 1 - int nSamples, cvlen; + int nSamples, vlen; // vlen : vector length void flushToTempFile() { std::streampos gap1 = std::streampos(fr) * FLOATSIZE; @@ -63,12 +65,12 @@ private: float *p = NULL; ftmpOut.seekp(0, std::ios::beg); - for (int i = 0; i < cvlen; i++) { + for (int i = 0; i < vlen; i++) { p = buffer + i; ftmpOut.seekp(gap1, std::ios::cur); for (int j = fr; j < to; j++) { ftmpOut.write((char*)p, FLOATSIZE); - p += cvlen; + p += vlen; } ftmpOut.seekp(gap2, std::ios::cur); }