This source file includes following definitions.
- commit
- createStruct
- getExtent
- getType
- newData
- SuppressWarnings
- newData
- SuppressWarnings
- getData
- SuppressWarnings
- getData
- SuppressWarnings
- getData
- SuppressWarnings
- getData
- addField
- setOffset
- addByte
- addByte
- addChar
- addChar
- addShort
- addShort
- addInt
- addInt
- addLong
- addLong
- addFloat
- addFloat
- addDouble
- addDouble
- addStruct
- addStruct
- addData
- addData
- validType
- getBuffer
- getByte
- getByte
- putByte
- putByte
- getChar
- getChar
- putChar
- putChar
- getShort
- getShort
- putShort
- putShort
- getInt
- getInt
- putInt
- putInt
- getLong
- getLong
- putLong
- putLong
- getFloat
- getFloat
- putFloat
- putFloat
- getDouble
- getDouble
- putDouble
- putDouble
- SuppressWarnings
- getData
- SuppressWarnings
- getData
- getBuffer
- getBuffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package mpi;
22
23 import java.nio.*;
24 import java.util.*;
25
26
27
28
29 public abstract class Struct
30 {
31 private int extent;
32 private ArrayList<Field> fields = new ArrayList<Field>();
33
34 private Datatype datatype, types[];
35 private int offsets[], lengths[];
36 private static final String typeMismatch = "Type mismatch";
37
38 private void commit() throws MPIException
39 {
40 if(datatype == null)
41 createStruct();
42 }
43
44 private void createStruct() throws MPIException
45 {
46 int count = fields.size();
47 types = new Datatype[count];
48 offsets = new int[count];
49 lengths = new int[count];
50
51 for(int i = 0; i < count; i++)
52 {
53 Field f = fields.get(i);
54
55 types[i] = f.type instanceof Struct ? ((Struct)f.type).datatype
56 : (Datatype)f.type;
57 offsets[i] = f.offset;
58 lengths[i] = f.length;
59 }
60
61 datatype = Datatype.createStruct(lengths, offsets, types);
62 datatype.commit();
63 extent = datatype.getExtent();
64 }
65
66
67
68
69
70
71 public final int getExtent() throws MPIException
72 {
73 commit();
74 return extent;
75 }
76
77
78
79
80
81
82 public final Datatype getType() throws MPIException
83 {
84 commit();
85 return datatype;
86 }
87
88
89
90
91
92 protected abstract Data newData();
93
94 @SuppressWarnings("unchecked")
95 private <T extends Data> T newData(ByteBuffer buffer, int offset)
96 {
97 Data d = newData();
98 d.buffer = buffer;
99 d.offset = offset;
100 return (T)d;
101 }
102
103 @SuppressWarnings("javadoc")
104
105
106
107
108
109
110 public final <T extends Data> T getData(ByteBuffer buffer) throws MPIException
111 {
112 commit();
113 return newData(buffer, 0);
114 }
115
116 @SuppressWarnings("javadoc")
117
118
119
120
121
122
123
124
125 public final <T extends Data> T getData(ByteBuffer buffer, int index)
126 throws MPIException
127 {
128 commit();
129 return newData(buffer, index * extent);
130 }
131
132 @SuppressWarnings("javadoc")
133
134
135
136
137
138
139 public final <T extends Data> T getData(byte[] array) throws MPIException
140 {
141 ByteBuffer buffer = ByteBuffer.wrap(array);
142 buffer.order(ByteOrder.nativeOrder());
143 return getData(buffer);
144 }
145
146 @SuppressWarnings("javadoc")
147
148
149
150
151
152
153
154
155 public final <T extends Data> T getData(byte[] array, int index)
156 throws MPIException
157 {
158 ByteBuffer buffer = ByteBuffer.wrap(array);
159 buffer.order(ByteOrder.nativeOrder());
160 return getData(buffer, index);
161 }
162
163 private int addField(Object type, int typeExtent, int length)
164 {
165 if(datatype != null)
166 throw new AssertionError("The struct data type was committed.");
167
168 int offset = extent;
169 extent += typeExtent * length;
170 fields.add(new Field(type, offset, length));
171 return offset;
172 }
173
174
175
176
177
178
179
180 public final Struct setOffset(int offset)
181 {
182 if(datatype != null)
183 throw new AssertionError("The struct data type was committed.");
184
185 if(offset < extent)
186 {
187 throw new IllegalArgumentException(
188 "The offset must be greater or equal to the accumulated extent.");
189 }
190
191 extent = offset;
192 return this;
193 }
194
195
196
197
198
199 public final int addByte()
200 {
201 return addByte(1);
202 }
203
204
205
206
207
208
209 public final int addByte(int length)
210 {
211 return addField(MPI.BYTE, 1, length);
212 }
213
214
215
216
217
218 public final int addChar()
219 {
220 return addChar(1);
221 }
222
223
224
225
226
227
228 public final int addChar(int length)
229 {
230 return addField(MPI.CHAR, 2, length);
231 }
232
233
234
235
236
237 public final int addShort()
238 {
239 return addShort(1);
240 }
241
242
243
244
245
246
247 public final int addShort(int length)
248 {
249 return addField(MPI.SHORT, 2, length);
250 }
251
252
253
254
255
256 public final int addInt()
257 {
258 return addInt(1);
259 }
260
261
262
263
264
265
266 public final int addInt(int length)
267 {
268 return addField(MPI.INT, 4, length);
269 }
270
271
272
273
274
275 public final int addLong()
276 {
277 return addLong(1);
278 }
279
280
281
282
283
284
285 public final int addLong(int length)
286 {
287 return addField(MPI.LONG, 8, length);
288 }
289
290
291
292
293
294 public final int addFloat()
295 {
296 return addFloat(1);
297 }
298
299
300
301
302
303
304 public final int addFloat(int length)
305 {
306 return addField(MPI.FLOAT, 4, length);
307 }
308
309
310
311
312
313 public final int addDouble()
314 {
315 return addDouble(1);
316 }
317
318
319
320
321
322
323 public final int addDouble(int length)
324 {
325 return addField(MPI.DOUBLE, 8, length);
326 }
327
328
329
330
331
332
333
334 public final int addStruct(Struct struct) throws MPIException
335 {
336 return addStruct(struct, 1);
337 }
338
339
340
341
342
343
344
345
346 public final int addStruct(Struct struct, int length) throws MPIException
347 {
348 struct.commit();
349 return addField(struct, struct.extent, length);
350 }
351
352
353
354
355
356
357
358 public final int addData(Datatype type) throws MPIException
359 {
360 return addData(type, 1);
361 }
362
363
364
365
366
367
368
369
370 public final int addData(Datatype type, int length) throws MPIException
371 {
372 return addField(type, type.getExtent() * type.baseSize, length);
373 }
374
375 private boolean validType(int fieldOffset, int index, Datatype type)
376 {
377 int i = Arrays.binarySearch(offsets, fieldOffset);
378 return index >= 0 && index < lengths[i] && type == types[i];
379 }
380
381 private static class Field
382 {
383 private Object type;
384 private int offset, length;
385
386 private Field(Object type, int offset, int length)
387 {
388 this.type = type;
389 this.offset = offset;
390 this.length = length;
391 }
392
393 }
394
395
396
397
398 public abstract class Data
399 {
400 private ByteBuffer buffer;
401 private int offset;
402
403
404
405
406
407
408 public final ByteBuffer getBuffer()
409 {
410 return offset == 0 ? buffer : MPI.slice(buffer, offset);
411 }
412
413
414
415
416
417
418 protected final byte getByte(int field)
419 {
420 assert validType(field, 0, MPI.BYTE) : typeMismatch;
421 return buffer.get(offset + field);
422 }
423
424
425
426
427
428
429
430 protected final byte getByte(int field, int index)
431 {
432 assert validType(field, index, MPI.BYTE) : typeMismatch;
433 return buffer.get(offset + field + index);
434 }
435
436
437
438
439
440
441 protected final void putByte(int field, byte v)
442 {
443 assert validType(field, 0, MPI.BYTE) : typeMismatch;
444 buffer.put(offset + field, v);
445 }
446
447
448
449
450
451
452
453 protected final void putByte(int field, int index, byte v)
454 {
455 assert validType(field, index, MPI.BYTE) : typeMismatch;
456 buffer.put(offset + field + index, v);
457 }
458
459
460
461
462
463
464 protected final char getChar(int field)
465 {
466 assert validType(field, 0, MPI.CHAR) : typeMismatch;
467 return buffer.getChar(offset + field);
468 }
469
470
471
472
473
474
475
476 protected final char getChar(int field, int index)
477 {
478 assert validType(field, index, MPI.CHAR) : typeMismatch;
479 return buffer.getChar(offset + field + index * 2);
480 }
481
482
483
484
485
486
487 protected final void putChar(int field, char v)
488 {
489 assert validType(field, 0, MPI.CHAR) : typeMismatch;
490 buffer.putChar(offset + field, v);
491 }
492
493
494
495
496
497
498
499 protected final void putChar(int field, int index, char v)
500 {
501 assert validType(field, index, MPI.CHAR) : typeMismatch;
502 buffer.putChar(offset + field + index * 2, v);
503 }
504
505
506
507
508
509
510 protected final short getShort(int field)
511 {
512 assert validType(field, 0, MPI.SHORT) : typeMismatch;
513 return buffer.getShort(offset + field);
514 }
515
516
517
518
519
520
521
522 protected final short getShort(int field, int index)
523 {
524 assert validType(field, index, MPI.SHORT) : typeMismatch;
525 return buffer.getShort(offset + field + index * 2);
526 }
527
528
529
530
531
532
533 protected final void putShort(int field, short v)
534 {
535 assert validType(field, 0, MPI.SHORT) : typeMismatch;
536 buffer.putShort(offset + field, v);
537 }
538
539
540
541
542
543
544
545 protected final void putShort(int field, int index, short v)
546 {
547 assert validType(field, index, MPI.SHORT) : typeMismatch;
548 buffer.putShort(offset + field + index * 2, v);
549 }
550
551
552
553
554
555
556 protected final int getInt(int field)
557 {
558 assert validType(field, 0, MPI.INT) : typeMismatch;
559 return buffer.getInt(offset + field);
560 }
561
562
563
564
565
566
567
568 protected final int getInt(int field, int index)
569 {
570 assert validType(field, index, MPI.INT) : typeMismatch;
571 return buffer.getInt(offset + field + index * 4);
572 }
573
574
575
576
577
578
579 protected final void putInt(int field, int v)
580 {
581 assert validType(field, 0, MPI.INT) : typeMismatch;
582 buffer.putInt(offset + field, v);
583 }
584
585
586
587
588
589
590
591 protected final void putInt(int field, int index, int v)
592 {
593 assert validType(field, index, MPI.INT) : typeMismatch;
594 buffer.putInt(offset + field + index * 4, v);
595 }
596
597
598
599
600
601
602 protected final long getLong(int field)
603 {
604 assert validType(field, 0, MPI.LONG) : typeMismatch;
605 return buffer.getLong(offset + field);
606 }
607
608
609
610
611
612
613
614 protected final long getLong(int field, int index)
615 {
616 assert validType(field, index, MPI.LONG) : typeMismatch;
617 return buffer.getLong(offset + field + index * 8);
618 }
619
620
621
622
623
624
625 protected final void putLong(int field, long v)
626 {
627 assert validType(field, 0, MPI.LONG) : typeMismatch;
628 buffer.putLong(offset + field, v);
629 }
630
631
632
633
634
635
636
637 protected final void putLong(int field, int index, long v)
638 {
639 assert validType(field, index, MPI.LONG) : typeMismatch;
640 buffer.putLong(offset + field + index * 8, v);
641 }
642
643
644
645
646
647
648 protected final float getFloat(int field)
649 {
650 assert validType(field, 0, MPI.FLOAT) : typeMismatch;
651 return buffer.getFloat(offset + field);
652 }
653
654
655
656
657
658
659
660 protected final float getFloat(int field, int index)
661 {
662 assert validType(field, index, MPI.FLOAT) : typeMismatch;
663 return buffer.getFloat(offset + field + index * 4);
664 }
665
666
667
668
669
670
671 protected final void putFloat(int field, float v)
672 {
673 assert validType(field, 0, MPI.FLOAT) : typeMismatch;
674 buffer.putFloat(offset + field, v);
675 }
676
677
678
679
680
681
682
683 protected final void putFloat(int field, int index, float v)
684 {
685 assert validType(field, index, MPI.FLOAT) : typeMismatch;
686 buffer.putFloat(offset + field + index * 4, v);
687 }
688
689
690
691
692
693
694 protected final double getDouble(int field)
695 {
696 assert validType(field, 0, MPI.DOUBLE) : typeMismatch;
697 return buffer.getDouble(offset + field);
698 }
699
700
701
702
703
704
705
706 protected final double getDouble(int field, int index)
707 {
708 assert validType(field, index, MPI.DOUBLE) : typeMismatch;
709 return buffer.getDouble(offset + field + index * 8);
710 }
711
712
713
714
715
716
717 protected final void putDouble(int field, double v)
718 {
719 assert validType(field, 0, MPI.DOUBLE) : typeMismatch;
720 buffer.putDouble(offset + field, v);
721 }
722
723
724
725
726
727
728
729 protected final void putDouble(int field, int index, double v)
730 {
731 assert validType(field, index, MPI.DOUBLE) : typeMismatch;
732 buffer.putDouble(offset + field + index * 8, v);
733 }
734
735 @SuppressWarnings("javadoc")
736
737
738
739
740
741
742 protected final <S extends Struct, D extends Struct.Data>
743 D getData(S struct, int field)
744 {
745 Struct s = (Struct)struct;
746 assert validType(field, 0, s.datatype) : typeMismatch;
747 return s.newData(buffer, offset + field);
748 }
749
750 @SuppressWarnings("javadoc")
751
752
753
754
755
756
757
758 protected final <S extends Struct, D extends Struct.Data>
759 D getData(S struct, int field, int index)
760 {
761 Struct s = (Struct)struct;
762 assert validType(field, index, s.datatype) : typeMismatch;
763 return s.newData(buffer, offset + field + index * s.extent);
764 }
765
766
767
768
769
770
771
772
773 protected final ByteBuffer getBuffer(Datatype type, int field)
774 {
775 assert validType(field, 0, type) : typeMismatch;
776 int position = offset + field;
777 return position == 0 ? buffer : MPI.slice(buffer, position);
778 }
779
780
781
782
783
784
785
786
787
788
789 protected final ByteBuffer getBuffer(Datatype type, int field, int index)
790 throws MPIException
791 {
792 assert validType(field, index, type) : typeMismatch;
793
794 int extent = type.getExtent() * type.baseSize,
795 position = offset + field + index * extent;
796
797 return position == 0 ? buffer : MPI.slice(buffer, position);
798 }
799
800 }
801
802 }