From d2e5ff1f1ac1048f388d768aa01dae46cb661dfc Mon Sep 17 00:00:00 2001
From: wfg <wfg@pc0098504.ornl.gov>
Date: Thu, 22 Dec 2016 14:12:01 -0500
Subject: [PATCH] Working on Capsule derived classes

Heap -> allocate capsule buffers in heap memory
ShmSystemV -> allocate capsule buffers in shared memory using System V
shm
---
 include/capsule/Heap.h       |  21 +++++--
 include/capsule/ShmSystemV.h | 104 +++++++++++++++++++++++++++++++++++
 include/core/Capsule.h       |   5 +-
 include/core/Transport.h     |   2 +-
 src/capsule/Heap.cpp         |  31 +++++++++++
 src/capsule/ShmSystemV.cpp   |  42 ++++++++++++++
 src/core/Capsule.cpp         |   5 +-
 7 files changed, 201 insertions(+), 9 deletions(-)
 create mode 100644 include/capsule/ShmSystemV.h
 create mode 100644 src/capsule/Heap.cpp
 create mode 100644 src/capsule/ShmSystemV.cpp

diff --git a/include/capsule/Heap.h b/include/capsule/Heap.h
index 0ae6a0dec..4af08b766 100644
--- a/include/capsule/Heap.h
+++ b/include/capsule/Heap.h
@@ -23,10 +23,20 @@ class Heap : public Capsule
 
 public:
 
-    std::vector<char> m_Buffer; ///< buffer allocated using the STL
-    std::vector<char> m_Metadata; ///< metadata buffer allocated using the STL
+    std::vector<char> m_Data; ///< data buffer allocated using the STL in heap memory
+    std::vector<char> m_Metadata; ///< metadata buffer allocated using the STL in heap memory
 
-    Heap( const std::string accessMode );
+    const size_t m_MaxDataSize; ///< maximum data size set by user
+    const size_t m_MaxMetadataSize; ///< maximum metadata size set by user
+
+    /**
+     * Unique constructor
+     * @param accessMode
+     * @param rankMPI
+     * @param dataSize maximum data size set by user
+     * @param metadataSize maximum metadata size set by user
+     */
+    Heap( const std::string accessMode, const int rankMPI, const size_t maxDataSize = 0, const size_t maxMetadataSize = 0 );
 
     ~Heap( );
 
@@ -82,9 +92,12 @@ public:
                 const std::vector<unsigned long long int>& globalDimensions,
                 const std::vector<unsigned long long int>& globalOffsets );
 
-
 };
 
+
+
+
+
 } //end namespace
 
 
diff --git a/include/capsule/ShmSystemV.h b/include/capsule/ShmSystemV.h
new file mode 100644
index 000000000..0ac7389b3
--- /dev/null
+++ b/include/capsule/ShmSystemV.h
@@ -0,0 +1,104 @@
+
+#ifndef SHMSYSTEMV_H_
+#define SHMSYSTEMV_H_
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+
+
+#include "core/Capsule.h"
+
+
+namespace adios
+{
+
+/**
+ * Buffer and Metadata are allocated in virtual memory using interprocess communication (IPC) of Unix's System V
+ */
+class ShmSystemV : public Capsule
+{
+
+public:
+
+    char* m_Data = nullptr; ///< reference to a shared memory data buffer created with shmget
+    size_t m_DataSize = 2147483648; ///< size of the allocated shared memory segment, needs a default (2 Gb?)
+    key_t m_DataKey; ///< key associated with the data buffer, created with ftok
+    int m_DataShmID; ///< data shared memory buffer id
+
+    char* m_Metadata = nullptr; ///< reference to a shared memory metadata buffer created with shmget
+    size_t m_MetadataSize = 1048576; ///< size of the allocated shared memory segment, needs a default ( 1 Mb?)
+    key_t m_MetadataKey; ///< key associated with the metadata buffer, created with ftok
+    int m_MetadataShmID; ///< metadata shared memory buffer id
+
+    /**
+     * Create a Capsule in shared memory
+     * @param accessMode
+     * @param pathName used to create the key as a unique identifier
+     * @param id used to create the key as a unique identifier, non-zero typically rank+1
+     * @param dataSize size of allocated memory segment for data
+     * @param metadataSize size of allocated memory segment for metadata
+     */
+    ShmSystemV( const std::string accessMode, const int rankMPI, const std::string pathName, const int id,
+                const size_t dataSize = 2147483648, const size_t metadataSize = 0 );
+
+    ~ShmSystemV( );
+
+
+    void Write( const Variable<char>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<unsigned char>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<short>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<unsigned short>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<unsigned int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<long int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<unsigned long int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<long long int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<unsigned long long int>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<float>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<double>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+    void Write( const Variable<long double>& variable, const std::vector<unsigned long long int>& localDimensions,
+                const std::vector<unsigned long long int>& globalDimensions,
+                const std::vector<unsigned long long int>& globalOffsets );
+
+};
+
+} //end namespace
+
+
+#endif /* SHMSYSTEMV_H_ */
diff --git a/include/core/Capsule.h b/include/core/Capsule.h
index 823a9dffc..891820d14 100644
--- a/include/core/Capsule.h
+++ b/include/core/Capsule.h
@@ -2,7 +2,7 @@
  * Capsule.h
  *
  *  Created on: Dec 7, 2016
- *      Author: wfg
+ *      Author: wfgtemplates and pointers
  */
 
 #ifndef CAPSULE_H_
@@ -27,13 +27,14 @@ public:
 
     const std::string m_Type; ///< buffer type
     const std::string m_AccessMode; ///< 'w': write, 'r': read, 'a': append
+    const int m_RankMPI;
 
     /**
      * Base class constructor providing type from derived class and accessMode
      * @param type derived class type
      * @param accessMode 'w':write, 'r':read, 'a':append
      */
-    Capsule( const std::string type, const std::string accessMode );
+    Capsule( const std::string type, const std::string accessMode, const int rankMPI );
 
     virtual ~Capsule( );
 
diff --git a/include/core/Transport.h b/include/core/Transport.h
index 1e79fc1c0..9097768ab 100644
--- a/include/core/Transport.h
+++ b/include/core/Transport.h
@@ -68,7 +68,7 @@ public:
     virtual void Write( const Capsule& capsule );
 
 
-    virtual void Close( const Capsule& capsule ) = 0; ///< closes current transport and flushes everything, can't be reachable after this call
+    virtual void Close( ) = 0; ///< closes current transport and flushes everything, can't be reachable after this call
 
 
 protected:
diff --git a/src/capsule/Heap.cpp b/src/capsule/Heap.cpp
new file mode 100644
index 000000000..4592e719f
--- /dev/null
+++ b/src/capsule/Heap.cpp
@@ -0,0 +1,31 @@
+/*
+ * Heap.cpp
+ *
+ *  Created on: Dec 22, 2016
+ *      Author: wfg
+ */
+
+
+#include "capsule/Heap.h"
+
+
+
+namespace adios
+{
+
+Heap::Heap( const std::string accessMode, const int rankMPI, const size_t maxDataSize, const size_t maxMetadataSize ):
+    Capsule( "Heap", accessMode, rankMPI ),
+    m_MaxDataSize{ maxDataSize },
+    m_MaxMetadataSize{ maxMetadataSize }
+{
+
+}
+
+
+Heap::~Heap( )
+{ }
+
+
+
+
+}  //end namespace
diff --git a/src/capsule/ShmSystemV.cpp b/src/capsule/ShmSystemV.cpp
new file mode 100644
index 000000000..37c4cf6ed
--- /dev/null
+++ b/src/capsule/ShmSystemV.cpp
@@ -0,0 +1,42 @@
+/*
+ * ShmSystemV.cpp
+ *
+ *  Created on: Dec 22, 2016
+ *      Author: wfg
+ */
+
+#include <sys/shm.h>
+
+#include "capsule/ShmSystemV.h"
+
+
+namespace adios
+{
+
+
+ShmSystemV::ShmSystemV( const std::string accessMode, const int rankMPI, const std::string pathName, const int id,
+                        const size_t dataSize, const size_t metadataSize ):
+    Capsule( "ShmSystemV", accessMode, rankMPI ),
+    m_DataSize{ dataSize },
+    m_MetadataSize{ metadataSize }
+{
+    // Data Shared memory sector
+    const std::string dataPath( pathName + "/adios.shm.data." + std::to_string( m_RankMPI ) );
+    m_DataKey = ftok( dataPath.c_str(), m_RankMPI+1 );
+    m_DataShmID = shmget( m_DataKey, m_DataSize, IPC_CREAT | 0666 );
+
+    // Metadata Shared memory sector
+    const std::string metadataPath( pathName + "/adios.shm.metadata." + std::to_string( m_RankMPI ) );
+    m_MetadataKey = ftok( metadataPath.c_str(), m_RankMPI+1 );
+    m_MetadataShmID = shmget( m_MetadataKey, m_MetadataSize, IPC_CREAT | 0666 );
+}
+
+
+ShmSystemV::~ShmSystemV( )
+{ }
+
+
+
+
+
+}
diff --git a/src/core/Capsule.cpp b/src/core/Capsule.cpp
index 1a8011fc5..65e9aabe0 100644
--- a/src/core/Capsule.cpp
+++ b/src/core/Capsule.cpp
@@ -13,9 +13,10 @@ namespace adios
 {
 
 
-Capsule::Capsule( const std::string type, const std::string accessMode ):
+Capsule::Capsule( const std::string type, const std::string accessMode, const int rankMPI ):
     m_Type{ type },
-    m_AccessMode{ accessMode }
+    m_AccessMode{ accessMode },
+    m_RankMPI{ rankMPI }
 { }
 
 
-- 
GitLab