• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/SpiNNaker-comms/src/main/java/uk/ac/manchester/spinnaker/transceiver/WriteMemoryFloodProcess.java
1
/*
2
 * Copyright (c) 2018 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.transceiver;
17

18
import static java.nio.ByteBuffer.allocate;
19
import static org.apache.commons.io.IOUtils.buffer;
20
import static uk.ac.manchester.spinnaker.messages.Constants.UDP_MESSAGE_MAX_SIZE;
21
import static uk.ac.manchester.spinnaker.utils.ByteBufferUtils.read;
22
import static uk.ac.manchester.spinnaker.utils.ByteBufferUtils.sliceUp;
23
import static uk.ac.manchester.spinnaker.utils.MathUtils.ceildiv;
24

25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.nio.ByteBuffer;
30

31
import uk.ac.manchester.spinnaker.connections.ConnectionSelector;
32
import uk.ac.manchester.spinnaker.connections.SCPConnection;
33
import uk.ac.manchester.spinnaker.machine.MemoryLocation;
34
import uk.ac.manchester.spinnaker.messages.scp.FloodFillData;
35
import uk.ac.manchester.spinnaker.messages.scp.FloodFillEnd;
36
import uk.ac.manchester.spinnaker.messages.scp.FloodFillStart;
37

38
/** A process for writing memory on multiple SpiNNaker chips at once. */
39
final class WriteMemoryFloodProcess extends TxrxProcess {
40
        /**
41
         * @param connectionSelector
42
         *            How to select how to communicate.
43
         * @param retryTracker
44
         *            Object used to track how many retries were used in an
45
         *            operation. May be {@code null} if no suck tracking is
46
         *            required.
47
         */
48
        WriteMemoryFloodProcess(
49
                        ConnectionSelector<? extends SCPConnection> connectionSelector,
50
                        RetryTracker retryTracker) {
51
                super(connectionSelector, retryTracker);
×
52
        }
×
53

54
        private static int numBlocks(int numBytes) {
55
                return ceildiv(numBytes, UDP_MESSAGE_MAX_SIZE);
×
56
        }
57

58
        /**
59
         * Flood fills memory with data from a buffer.
60
         *
61
         * @param nearestNeighbourID
62
         *            The ID of the fill
63
         * @param baseAddress
64
         *            Where the data is to be written to
65
         * @param data
66
         *            The data, from the <i>position</i> (inclusive) to the
67
         *            <i>limit</i> (exclusive)
68
         * @throws IOException
69
         *             If anything goes wrong with networking.
70
         * @throws ProcessException
71
         *             If SpiNNaker rejects a message.
72
         * @throws InterruptedException
73
         *             If the communications were interrupted.
74
         */
75
        void writeMemory(byte nearestNeighbourID, MemoryLocation baseAddress,
76
                        ByteBuffer data)
77
                        throws IOException, ProcessException, InterruptedException {
78
                int numBytes = data.remaining();
×
79
                call(new FloodFillStart(nearestNeighbourID, numBlocks(numBytes)));
×
80

81
                int blockID = 0;
×
82
                for (var slice : sliceUp(data, UDP_MESSAGE_MAX_SIZE)) {
×
83
                        sendRequest(new FloodFillData(nearestNeighbourID, blockID++,
×
84
                                        baseAddress, slice));
85
                        baseAddress = baseAddress.add(slice.remaining());
×
86
                }
×
87
                finishBatch();
×
88

89
                call(new FloodFillEnd(nearestNeighbourID));
×
90
        }
×
91

92
        /**
93
         * Flood fills memory with data from an input stream.
94
         *
95
         * @param nearestNeighbourID
96
         *            The ID of the fill
97
         * @param baseAddress
98
         *            Where the data is to be written to
99
         * @param dataStream
100
         *            The place to get the data from.
101
         * @param numBytes
102
         *            The number of bytes to read. Be aware that you can specify a
103
         *            number larger than the number of bytes actually available; if
104
         *            you do so, the fill will terminate early and this may cause
105
         *            problems.
106
         * @throws IOException
107
         *             If anything goes wrong with networking or the input stream.
108
         * @throws ProcessException
109
         *             If SpiNNaker rejects a message.
110
         * @throws InterruptedException
111
         *             If the communications were interrupted.
112
         */
113
        void writeMemory(byte nearestNeighbourID, MemoryLocation baseAddress,
114
                        InputStream dataStream, int numBytes)
115
                        throws IOException, ProcessException, InterruptedException {
116
                call(new FloodFillStart(nearestNeighbourID, numBlocks(numBytes)));
×
117

118
                int blockID = 0;
×
119
                while (numBytes > 0) {
×
120
                        // Allocate a new buffer each time; assume message holds ref to it
121
                        var tmp =
×
122
                                        read(dataStream, allocate(UDP_MESSAGE_MAX_SIZE), numBytes);
×
123
                        if (tmp == null) {
×
124
                                break;
×
125
                        }
126
                        sendRequest(new FloodFillData(nearestNeighbourID, blockID,
×
127
                                        baseAddress, tmp));
128
                        blockID++;
×
129
                        numBytes -= tmp.remaining();
×
130
                        baseAddress = baseAddress.add(tmp.remaining());
×
131
                }
×
132
                finishBatch();
×
133

134
                call(new FloodFillEnd(nearestNeighbourID));
×
135
        }
×
136

137
        /**
138
         * Flood fills memory with data from a file.
139
         *
140
         * @param nearestNeighbourID
141
         *            The ID of the fill
142
         * @param baseAddress
143
         *            Where the data is to be written to
144
         * @param dataFile
145
         *            The data in a file, which will be fully transferred.
146
         * @throws IOException
147
         *             If anything goes wrong with networking or access to the file.
148
         * @throws ProcessException
149
         *             If SpiNNaker rejects a message.
150
         * @throws InterruptedException
151
         *             If the communications were interrupted.
152
         */
153
        void writeMemory(byte nearestNeighbourID, MemoryLocation baseAddress,
154
                        File dataFile)
155
                        throws IOException, ProcessException, InterruptedException {
156
                try (var s = buffer(new FileInputStream(dataFile))) {
×
157
                        writeMemory(nearestNeighbourID, baseAddress, s,
×
158
                                        (int) dataFile.length());
×
159
                }
160
        }
×
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc