Permalink
Browse files

*** empty log message ***

  • Loading branch information...
1 parent a0aa284 commit 26cbfa51f68997f62b19142e53a806afac442078 langmead committed Sep 2, 2009
Showing with 119 additions and 120 deletions.
  1. +4 −5 ebwt.h
  2. +2 −3 reference.h
  3. +1 −103 shmem.cpp
  4. +112 −9 shmem.h
View
9 ebwt.h
@@ -2955,8 +2955,8 @@ void Ebwt<TStr>::readIntoMemory(bool justHeader,
}
bool shmemLeader = true;
if(useShmem_) {
- shmemLeader = ALLOC_SHARED(
- _in1Str + "[ebwt]", eh->_ebwtTotLen, (void**)&this->_ebwt,
+ shmemLeader = ALLOC_SHARED_U8(
+ _in1Str + "[ebwt]", eh->_ebwtTotLen, &this->_ebwt,
"ebwt[]", _verbose || startVerbose);
if(_verbose || startVerbose) {
cerr << " shared-mem " << (shmemLeader ? "leader" : "follower") << endl;
@@ -3121,9 +3121,8 @@ void Ebwt<TStr>::readIntoMemory(bool justHeader,
exit(1);
}
} else {
- shmemLeader = ALLOC_SHARED(
- _in2Str + "[offs]", offsLenSampled*4,
- reinterpret_cast<void**>(&this->_offs),
+ shmemLeader = ALLOC_SHARED_U32(
+ _in2Str + "[offs]", offsLenSampled*4, &this->_offs,
"offs", _verbose || startVerbose);
}
}
View
@@ -215,9 +215,8 @@ class BitPairReference {
exit(1);
}
} else {
- shmemLeader = ALLOC_SHARED(
- s4 + "[ref]", (cumsz >> 2),
- reinterpret_cast<void**>(&buf_),
+ shmemLeader = ALLOC_SHARED_U8(
+ s4 + "[ref]", (cumsz >> 2), &buf_,
"ref", verbose_ || startVerbose);
}
if(shmemLeader) {
View
104 shmem.cpp
@@ -12,112 +12,10 @@
#include <unistd.h>
#include <sys/shm.h>
#include <errno.h>
-#include "str_util.h"
+#include "shmem.h"
using namespace std;
-#define SHMEM_UNINIT 0xafba4242
-#define SHMEM_INIT 0xffaa6161
-
-/**
- * Tries to allocate a shared-memory chunk for a given file of a given size.
- */
-bool allocSharedMem(string fname,
- size_t len,
- void ** dst,
- const char *memName,
- bool verbose)
-{
- int shmid = -1;
- // Calculate key given string
- key_t key = (key_t)hash_string(fname);
- shmid_ds ds;
- int ret;
- // Reserve 4 bytes at the end for silly synchronization
- size_t shmemLen = len + 4;
- if(verbose) {
- cerr << "Reading " << len << "+4 bytes into shared memory for " << memName << endl;
- }
- void *ptr = NULL;
- while(true) {
- // Create the shrared-memory block
- if((shmid = shmget(key, shmemLen, IPC_CREAT | 0666)) < 0) {
- if(errno == ENOMEM) {
- cerr << "Out of memory allocating shared area " << memName << endl;
- } else if(errno == EACCES) {
- cerr << "EACCES" << endl;
- } else if(errno == EEXIST) {
- cerr << "EEXIST" << endl;
- } else if(errno == EINVAL) {
- cerr << "Warning: shared-memory chunk's segment size doesn't match expected size (" << (shmemLen) << ")" << endl
- << "Deleteing old shared memory block and trying again." << endl;
- shmid = shmget(key, 0, 0);
- if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
- cerr << "shmctl returned " << ret
- << " for IPC_RMID, errno is " << errno
- << ", shmid is " << shmid << endl;
- exit(1);
- } else {
- cerr << "Deleted shared mem chunk with shmid " << shmid << endl;
- }
- continue;
- } else if(errno == ENOENT) {
- cerr << "ENOENT" << endl;
- } else if(errno == ENOSPC) {
- cerr << "ENOSPC" << endl;
- } else {
- cerr << "shmget returned " << shmid << " for and errno is " << errno << endl;
- }
- exit(1);
- }
- ptr = shmat(shmid, 0, 0);
- if(ptr == (void*)-1) {
- cerr << "Failed to attach " << memName << " to shared memory with shmat()." << endl;
- exit(1);
- }
- if(ptr == NULL) {
- cerr << memName << " pointer returned by shmat() was NULL." << endl;
- exit(1);
- }
- // Did I create it, or did I just attach to one created by
- // another process?
- if((ret = shmctl(shmid, IPC_STAT, &ds)) < 0) {
- cerr << "shmctl returned " << ret << " for IPC_STAT and errno is " << errno << endl;
- exit(1);
- }
- if(ds.shm_segsz != shmemLen) {
- cerr << "Warning: shared-memory chunk's segment size (" << ds.shm_segsz
- << ") doesn't match expected size (" << shmemLen << ")" << endl
- << "Deleteing old shared memory block and trying again." << endl;
- if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
- cerr << "shmctl returned " << ret << " for IPC_RMID and errno is " << errno << endl;
- exit(1);
- }
- } else {
- break;
- }
- } // while(true)
- *dst = ptr;
- bool initid = (((volatile uint32_t*)((char*)ptr + len))[0] == SHMEM_INIT);
- if(ds.shm_cpid == getpid() && !initid) {
- if(verbose) {
- cerr << " I (pid = " << getpid() << ") created the "
- << "shared memory for " << memName << endl;
- }
- // Set this value just off the end of the chunk to
- // indicate that the data hasn't been read yet.
- ((volatile uint32_t*)((char*)ptr + len))[0] = SHMEM_UNINIT;
- return true;
- } else {
- if(verbose) {
- cerr << " I (pid = " << getpid()
- << ") did not create the shared memory for "
- << memName << ". Pid " << ds.shm_cpid << " did." << endl;
- }
- return false;
- }
-}
-
/**
* Notify other users of a shared-memory chunk that the leader has
* finished initializing it.
View
121 shmem.h
@@ -12,26 +12,129 @@
#include <string>
#include <sys/shm.h>
-
-extern bool allocSharedMem(
- std::string fname,
- size_t len,
- void ** dst,
- const char *memName,
- bool verbose);
+#include <unistd.h>
+#include <sys/shm.h>
+#include <errno.h>
+#include "str_util.h"
extern void notifySharedMem(void *mem, size_t len);
extern void waitSharedMem(void *mem, size_t len);
-#define ALLOC_SHARED allocSharedMem
+#define ALLOC_SHARED_U8 allocSharedMem<uint8_t>
+#define ALLOC_SHARED_U32 allocSharedMem<uint32_t>
#define FREE_SHARED shmdt
#define NOTIFY_SHARED notifySharedMem
#define WAIT_SHARED waitSharedMem
+#define SHMEM_UNINIT 0xafba4242
+#define SHMEM_INIT 0xffaa6161
+
+/**
+ * Tries to allocate a shared-memory chunk for a given file of a given size.
+ */
+template <typename T>
+bool allocSharedMem(std::string fname,
+ size_t len,
+ T ** dst,
+ const char *memName,
+ bool verbose)
+{
+ using namespace std;
+ int shmid = -1;
+ // Calculate key given string
+ key_t key = (key_t)hash_string(fname);
+ shmid_ds ds;
+ int ret;
+ // Reserve 4 bytes at the end for silly synchronization
+ size_t shmemLen = len + 4;
+ if(verbose) {
+ cerr << "Reading " << len << "+4 bytes into shared memory for " << memName << endl;
+ }
+ T *ptr = NULL;
+ while(true) {
+ // Create the shrared-memory block
+ if((shmid = shmget(key, shmemLen, IPC_CREAT | 0666)) < 0) {
+ if(errno == ENOMEM) {
+ cerr << "Out of memory allocating shared area " << memName << endl;
+ } else if(errno == EACCES) {
+ cerr << "EACCES" << endl;
+ } else if(errno == EEXIST) {
+ cerr << "EEXIST" << endl;
+ } else if(errno == EINVAL) {
+ cerr << "Warning: shared-memory chunk's segment size doesn't match expected size (" << (shmemLen) << ")" << endl
+ << "Deleteing old shared memory block and trying again." << endl;
+ shmid = shmget(key, 0, 0);
+ if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
+ cerr << "shmctl returned " << ret
+ << " for IPC_RMID, errno is " << errno
+ << ", shmid is " << shmid << endl;
+ exit(1);
+ } else {
+ cerr << "Deleted shared mem chunk with shmid " << shmid << endl;
+ }
+ continue;
+ } else if(errno == ENOENT) {
+ cerr << "ENOENT" << endl;
+ } else if(errno == ENOSPC) {
+ cerr << "ENOSPC" << endl;
+ } else {
+ cerr << "shmget returned " << shmid << " for and errno is " << errno << endl;
+ }
+ exit(1);
+ }
+ ptr = (T*)shmat(shmid, 0, 0);
+ if(ptr == (void*)-1) {
+ cerr << "Failed to attach " << memName << " to shared memory with shmat()." << endl;
+ exit(1);
+ }
+ if(ptr == NULL) {
+ cerr << memName << " pointer returned by shmat() was NULL." << endl;
+ exit(1);
+ }
+ // Did I create it, or did I just attach to one created by
+ // another process?
+ if((ret = shmctl(shmid, IPC_STAT, &ds)) < 0) {
+ cerr << "shmctl returned " << ret << " for IPC_STAT and errno is " << errno << endl;
+ exit(1);
+ }
+ if(ds.shm_segsz != shmemLen) {
+ cerr << "Warning: shared-memory chunk's segment size (" << ds.shm_segsz
+ << ") doesn't match expected size (" << shmemLen << ")" << endl
+ << "Deleteing old shared memory block and trying again." << endl;
+ if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
+ cerr << "shmctl returned " << ret << " for IPC_RMID and errno is " << errno << endl;
+ exit(1);
+ }
+ } else {
+ break;
+ }
+ } // while(true)
+ *dst = ptr;
+ bool initid = (((volatile uint32_t*)((char*)ptr + len))[0] == SHMEM_INIT);
+ if(ds.shm_cpid == getpid() && !initid) {
+ if(verbose) {
+ cerr << " I (pid = " << getpid() << ") created the "
+ << "shared memory for " << memName << endl;
+ }
+ // Set this value just off the end of the chunk to
+ // indicate that the data hasn't been read yet.
+ ((volatile uint32_t*)((char*)ptr + len))[0] = SHMEM_UNINIT;
+ return true;
+ } else {
+ if(verbose) {
+ cerr << " I (pid = " << getpid()
+ << ") did not create the shared memory for "
+ << memName << ". Pid " << ds.shm_cpid << " did." << endl;
+ }
+ return false;
+ }
+}
+
#else
-#define ALLOC_SHARED(...)
+#define ALLOC_SHARED_U8(...)
+#define ALLOC_SHARED_U32(...)
#define FREE_SHARED(...)
#define NOTIFY_SHARED(...)
#define WAIT_SHARED(...)

0 comments on commit 26cbfa5

Please sign in to comment.