Skip to content

Commit bcb3f00

Browse files
committed
keep adding the docs
1 parent 6c5a407 commit bcb3f00

File tree

5 files changed

+101
-31
lines changed

5 files changed

+101
-31
lines changed

src/main/java/io/bioimage/modelrunner/runmode/RunMode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void addCodeToRecreateTensor(String ogName, SharedMemoryArray shma, Tensor<T> te
377377
// buffer=input0_appose_shm.buf).reshape([64, 64]),
378378
// dims=["b", "c", "y", "x"], name="input0")
379379
shmInstancesCode += ogName + APPOSE_SHM_KEY + " = shared_memory.SharedMemory(name='"
380-
+ shma.getMemoryLocationPythonName() + "', size=" + shma.getSize() + ")" + System.lineSeparator();
380+
+ shma.getNameForPython() + "', size=" + shma.getSize() + ")" + System.lineSeparator();
381381
shmInstancesCode += "shm_out_list.append(" + ogName + APPOSE_SHM_KEY + ")" + System.lineSeparator();
382382
shmInstancesCode += ogName + APPOSE_SHM_KEY + ".unlink()" + System.lineSeparator();
383383
int size = 1;
@@ -409,7 +409,7 @@ void addCodeToRecreateNumpyArray(String ogName, SharedMemoryArray shma, RandomAc
409409
// input0_appose_shm = shared_memory.SharedMemory(name=input0)
410410
// input0 = np.ndarray(size, dtype="float64", buffer=input0_appose_shm.buf).reshape([64, 64])
411411
shmInstancesCode += ogName + APPOSE_SHM_KEY + " = shared_memory.SharedMemory(name='"
412-
+ shma.getMemoryLocationPythonName() + "', size=" + shma.getSize() + ")" + System.lineSeparator();
412+
+ shma.getNameForPython() + "', size=" + shma.getSize() + ")" + System.lineSeparator();
413413
shmInstancesCode += "shm_out_list.append(" + ogName + APPOSE_SHM_KEY + ")" + System.lineSeparator();
414414
shmInstancesCode += ogName + APPOSE_SHM_KEY + ".unlink()" + System.lineSeparator();
415415
int size = 1;

src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArray.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
* @author Carlos Garcia Lopez de Haro
3838
*/
3939
public interface SharedMemoryArray extends Closeable {
40+
41+
42+
public static final int O_RDONLY = 0;
43+
public static final int O_RDWR = 2; // Read-write mode
44+
public static final int O_CREAT = 64; // Create if it does not exist
45+
public static final int PROT_READ = 0x1; // Page can be read
46+
public static final int PROT_WRITE = 0x2; // Page can be written
47+
public static final int MAP_SHARED = 0x01; // Share changes
4048

4149
final static String[] SPECIAL_CHARS_LIST = new String[] {"/", "\\", "#", "·", "!", "¡", "¿", "?", "@", "|", "$", ">", "<", ";"};
4250

@@ -131,18 +139,47 @@ static String createShmName() {
131139
else return ("/shm-" + UUID.randomUUID()).substring(0, SharedMemoryArrayMacOS.MACOS_MAX_LENGTH);
132140
}
133141

134-
public String getMemoryLocationName();
142+
/**
143+
*
144+
* @return
145+
*/
146+
public String getName();
135147

136-
public String getMemoryLocationPythonName();
148+
/**
149+
*
150+
* @return
151+
*/
152+
public String getNameForPython();
137153

154+
/**
155+
*
156+
* @return
157+
*/
138158
public Pointer getPointer();
139159

160+
/**
161+
*
162+
* @return
163+
*/
140164
public int getSize();
141165

166+
/**
167+
*
168+
* @return
169+
*/
142170
public String getOriginalDataType();
143171

172+
/**
173+
*
174+
* @return
175+
*/
144176
public long[] getOriginalShape();
145177

178+
/**
179+
*
180+
* @param <T>
181+
* @return
182+
*/
146183
public <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> getSharedRAI();
147184

148185
/**

src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayLinux.java

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.ByteArrayInputStream;
2424
import java.nio.ByteBuffer;
2525
import java.util.HashMap;
26-
import java.util.UUID;
2726

2827
import com.sun.jna.Pointer;
2928

@@ -58,29 +57,35 @@
5857
*/
5958
public final class SharedMemoryArrayLinux implements SharedMemoryArray
6059
{
60+
/**
61+
* Instance of the CLibrary JNI containing the methods to interact with the Shared memory segments
62+
*/
6163
private static final CLibrary INSTANCE = CLibrary.INSTANCE;
6264
/**
63-
* Shared memory location
65+
* File descriptor value of the shared memory segment
6466
*/
6567
private final int shmFd;
6668
/**
67-
*
69+
* Pointer referencing the shared memory byte array
6870
*/
6971
private Pointer pSharedMemory;
7072
/**
71-
* Name defining the location of the shared memory block
73+
* Name of the file containing the shared memory segment. In Unix based systems consits of "/" + file_name.
74+
* In Linux the shared memory segments can be inspected at /dev/shm
7275
*/
7376
private final String memoryName;
7477
/**
7578
* Size of the shared memory block
7679
*/
7780
private int size;
7881
/**
79-
* Datatype of the shm array
82+
* Shared memory segments store bytes. This field represents the original data type of the array that was written
83+
* into the bytes of the shared memory segment. It is helful to retrieve the object later.
8084
*/
8185
private final String originalDataType;
8286
/**
83-
* Original dimensions of the shm array
87+
* Shared memory segments are flat arrays, only one dimension. This field keeps the dimensions of the array before
88+
* flattening it and copying it to the shared memory.
8489
*/
8590
private final long[] originalDims;
8691
/**
@@ -93,19 +98,39 @@ public final class SharedMemoryArrayLinux implements SharedMemoryArray
9398
* of bytes corresponding to the values of the array, no header
9499
*/
95100
private boolean isNumpyFormat = false;
96-
97-
public static final int O_RDONLY = 0;
98-
static final int O_RDWR = 2; // Read-write mode
99-
static final int O_CREAT = 64; // Create if it does not exist
100-
private static final int PROT_READ = 0x1; // Page can be read
101-
private static final int PROT_WRITE = 0x2; // Page can be written
102-
private static final int MAP_SHARED = 0x01; // Share changes
103101

102+
/**
103+
* Create a shared memory segment with the wanted size, where an object of a certain datatype and
104+
* share is going to be stored.
105+
* Unless the array of bytes that is going to be written into the shared memory segment has numpy format,
106+
* the size parameter should only depend on the shape and the data type.
107+
* The name of the file containing the shared memory segment is assigned automatically.
108+
* @param size
109+
* number of bytes that are going to be written into the shared memory
110+
* @param dtype
111+
* data type of the object that is going to be written into the shared memory
112+
* @param shape
113+
* shape (array dimensions) of the array that is going to be flattened and written into the shared memory segment
114+
*/
104115
private SharedMemoryArrayLinux(int size, String dtype, long[] shape)
105116
{
106-
this("/shm-" + UUID.randomUUID(), size, dtype, shape);
117+
this(SharedMemoryArray.createShmName(), size, dtype, shape);
107118
}
108-
119+
120+
/**
121+
* Create a shared memory segment with the wanted size, where an object of a certain datatype and
122+
* share is going to be stored. The shared memory name is created in the location of the name provided
123+
* Unless the array of bytes that is going to be written into the shared memory segment has numpy format,
124+
* the size parameter should only depend on the shape and the data type.
125+
* @param name
126+
* name of the file name that is going to be used to identify the shared memory segment
127+
* @param size
128+
* number of bytes that are going to be written into the shared memory
129+
* @param dtype
130+
* data type of the object that is going to be written into the shared memory
131+
* @param shape
132+
* shape (array dimensions) of the array that is going to be flattened and written into the shared memory segment
133+
*/
109134
private SharedMemoryArrayLinux(String name, int size, String dtype, long[] shape)
110135
{
111136
this.originalDataType = dtype;
@@ -130,22 +155,30 @@ private SharedMemoryArrayLinux(String name, int size, String dtype, long[] shape
130155
}
131156
}
132157

133-
public String getMemoryLocationName() {
158+
/**
159+
* {@inheritDoc}
160+
*/
161+
public String getName() {
134162
return this.memoryName;
135163
}
136164

137-
public String getMemoryLocationPythonName() {
165+
/**
166+
* {@inheritDoc}
167+
*/
168+
public String getNameForPython() {
138169
return this.memoryName.substring("/".length());
139170
}
140171

172+
/**
173+
* {@inheritDoc}
174+
*/
141175
public Pointer getPointer() {
142176
return this.pSharedMemory;
143177
}
144178

145-
public int getSharedMemoryBlock() {
146-
return this.shmFd;
147-
}
148-
179+
/**
180+
* {@inheritDoc}
181+
*/
149182
public int getSize() {
150183
return this.size;
151184
}
@@ -167,7 +200,7 @@ public int getSize() {
167200
*/
168201
protected static <T extends RealType<T> & NativeType<T>> SharedMemoryArrayLinux build(RandomAccessibleInterval<T> rai)
169202
{
170-
return build("/shm-" + UUID.randomUUID(), rai);
203+
return build(SharedMemoryArray.createShmName(), rai);
171204
}
172205

173206
/**
@@ -262,7 +295,7 @@ protected static <T extends RealType<T> & NativeType<T>> SharedMemoryArrayLinux
262295
*/
263296
protected static <T extends RealType<T> & NativeType<T>> SharedMemoryArrayLinux buildNumpyFormat(RandomAccessibleInterval<T> rai)
264297
{
265-
return buildNumpyFormat("/shm-" + UUID.randomUUID(), rai);
298+
return buildNumpyFormat(SharedMemoryArray.createShmName(), rai);
266299
}
267300

268301
/**

src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayMacOS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ private SharedMemoryArrayMacOS(String name, int size, String dtype, long[] shape
131131
}
132132
}
133133

134-
public String getMemoryLocationName() {
134+
public String getName() {
135135
return this.memoryName;
136136
}
137137

138-
public String getMemoryLocationPythonName() {
138+
public String getNameForPython() {
139139
return this.memoryName.substring("/".length());
140140
}
141141

src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayWin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ private SharedMemoryArrayWin(String name, int size, String dtype, long[] shape)
136136
}
137137
}
138138

139-
public String getMemoryLocationName() {
139+
public String getName() {
140140
return this.memoryName;
141141
}
142142

143-
public String getMemoryLocationPythonName() {
143+
public String getNameForPython() {
144144
return this.memoryName.substring("Local\\".length());
145145
}
146146

0 commit comments

Comments
 (0)