File indexing completed on 2025-01-18 09:58:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #ifndef G4GEOMSPLITTER_HH
0037 #define G4GEOMSPLITTER_HH
0038
0039 #include "globals.hh"
0040 #include "geomwdefs.hh"
0041 #include "G4AutoLock.hh"
0042
0043 template <class T>
0044 class G4GeomSplitter
0045 {
0046 public:
0047
0048 G4GeomSplitter()
0049 : sharedOffset(nullptr)
0050 {
0051 G4MUTEXINIT(mutex);
0052 }
0053
0054 T* Reallocate(G4int size)
0055 {
0056 totalspace = size;
0057 return (T *) std::realloc(offset, totalspace * sizeof(T));
0058 }
0059
0060 G4int CreateSubInstance()
0061
0062
0063 {
0064 G4AutoLock l(&mutex);
0065 ++totalobj;
0066 if (totalobj > totalspace)
0067 {
0068 offset = Reallocate(totalspace+512);
0069 if (offset == nullptr)
0070 {
0071 G4Exception("G4GeomSPlitter::CreateSubInstance()",
0072 "OutOfMemory", FatalException, "Cannot malloc space!");
0073 }
0074 sharedOffset = offset;
0075 }
0076 return (totalobj - 1);
0077 }
0078
0079 void CopyMasterContents()
0080 {
0081 G4AutoLock l(&mutex);
0082 std::memcpy(offset, sharedOffset, totalspace * sizeof(T));
0083 }
0084
0085 void SlaveCopySubInstanceArray()
0086
0087
0088 {
0089 G4AutoLock l(&mutex);
0090 if (offset != nullptr) { return; }
0091 offset = Reallocate(totalspace);
0092 if (offset == nullptr)
0093 {
0094 G4Exception("G4GeomSplitter::SlaveCopySubInstanceArray()",
0095 "OutOfMemory", FatalException, "Cannot malloc space!");
0096 }
0097 l.unlock();
0098 CopyMasterContents();
0099 }
0100
0101 void SlaveInitializeSubInstance()
0102
0103
0104
0105 {
0106 G4AutoLock l(&mutex);
0107 if (offset != nullptr) { return; }
0108 offset = Reallocate(totalspace);
0109
0110 if (offset == nullptr)
0111 {
0112 G4Exception("G4GeomSplitter::SlaveInitializeSubInstance()",
0113 "OutOfMemory", FatalException, "Cannot malloc space!");
0114 }
0115
0116 for (G4int i=0 ; i<totalspace; ++i)
0117 {
0118 offset[i].initialize();
0119 }
0120 }
0121
0122 void SlaveReCopySubInstanceArray()
0123
0124
0125
0126
0127 {
0128 if (offset == nullptr)
0129 {
0130 SlaveInitializeSubInstance();
0131 G4Exception("G4GeomSPlitter::SlaveReCopySubInstance()",
0132 "MissingInitialisation", JustWarning,
0133 "Must be called after Initialisation or first Copy.");
0134 }
0135 CopyMasterContents();
0136 }
0137
0138 void FreeSlave()
0139
0140 {
0141 if (offset == nullptr) { return; }
0142 std::free( offset );
0143 offset = nullptr;
0144 }
0145
0146
0147
0148 T* GetOffset() { return offset; }
0149
0150 void UseWorkArea( T* newOffset )
0151
0152 {
0153 if( (offset!=nullptr) && (offset!=newOffset) )
0154 {
0155 G4Exception("G4GeomSplitter::UseWorkspace()",
0156 "TwoWorkspaces", FatalException,
0157 "Thread already has workspace - cannot use another.");
0158 }
0159 offset= newOffset;
0160 }
0161
0162 T* FreeWorkArea()
0163
0164
0165 {
0166 T* offsetRet = offset;
0167 offset = nullptr;
0168 return offsetRet;
0169 }
0170
0171 public:
0172
0173 G4GEOM_DLL static G4ThreadLocal T* offset;
0174
0175 private:
0176
0177 G4int totalobj{0};
0178 G4int totalspace{0};
0179 T* sharedOffset;
0180 G4Mutex mutex;
0181 };
0182
0183 template <typename T> G4ThreadLocal T* G4GeomSplitter<T>::offset = nullptr;
0184
0185 #endif