1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
//******************************************************************************
//
// File: SpinRelaxometryClu.java
// Package: edu.rit.mri
// Unit: Class edu.rit.mri.SpinRelaxometryClu
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.mri;
import edu.rit.io.Files;
import edu.rit.mp.ObjectBuf;
import edu.rit.mp.buf.ObjectItemBuf;
import edu.rit.numeric.ArraySeries;
import edu.rit.numeric.Series;
import edu.rit.pj.Comm;
import edu.rit.pj.CommStatus;
import edu.rit.pj.IntegerSchedule;
import edu.rit.pj.ParallelRegion;
import edu.rit.pj.ParallelSection;
import edu.rit.pj.ParallelTeam;
import edu.rit.util.Range;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* Class SpinRelaxometryClu is a cluster parallel program that does a spin
* relaxometry analysis of one or more magnetic resonance images. Each MR
* image's spin signal data set is stored in a file as defined in class
* {@linkplain SignalDataSetWriter}. The program does the following for each
* spin signal data set file. Using an instance of class {@linkplain
* SignalDataSetReader}, the program reads the spin signal data for each pixel
* in the image. Using class {@linkplain PixelAnalysis}, the program does a spin
* relaxometry analysis on each pixel and computes the tissues for each pixel.
* Using an instance of class {@linkplain TissuesDataSetWriter}, the program
* writes the tissues data into another file. For example, if the input spin
* signal data set files are named <TT>image1.dat</TT>, <TT>image2.dat</TT>, and
* so on, the output tissues data set files are named
* <TT>tissues_image1.dat</TT>, <TT>tissues_image2.dat</TT>, and so on.
* <P>
* The program uses the master-worker pattern for load balancing. The program
* uses the parallel input files pattern for reduced message passing. The master
* partitions each image into chunks of 100 pixels and sends the chunks to the
* workers. In parallel, the workers read the input spin signal data set file,
* do the spin relaxometry analysis calculations for each pixel in the chunk,
* and send the calculated tissues data back to the master. The master writes
* the tissues data into the output tissues data set file.
* <P>
* Usage: java -Dpj.np=<I>K</I> edu.rit.mri.SpinRelaxometryClu <I>R1_lower</I>
* <I>R1_upper</I> <I>N</I> <I>signalfile</I> [ <I>signalfile</I> . . . ]
* <BR><I>K</I> = Number of parallel processes
* <BR><I>R1_lower</I> = Lower <I>R</I>1 spin-lattice relaxation rate (1/sec)
* <BR><I>R1_upper</I> = Upper <I>R</I>1 spin-lattice relaxation rate (1/sec)
* <BR><I>N</I> = Number of <I>R</I>1 intervals
* <BR><I>signalfile</I> = Input spin signal data set file
*
* @author Alan Kaminsky
* @version 25-Jun-2008
*/
public class SpinRelaxometryClu
{
// Global variables.
// World communicator.
static Comm world;
static int size;
static int rank;
// Command line arguments.
static double R1_lower;
static double R1_upper;
static int N;
static String[] signalfilename;
// Prevent construction.
private SpinRelaxometryClu()
{
}
// Main program.
/**
* Main program.
*/
public static void main
(String[] args)
throws Exception
{
// Start timing.
long t1 = System.currentTimeMillis();
// Initialize world communicator.
Comm.init (args);
world = Comm.world();
size = world.size();
rank = world.rank();
// Parse command line arguments.
if (args.length < 4) usage();
R1_lower = Double.parseDouble (args[0]);
R1_upper = Double.parseDouble (args[1]);
N = Integer.parseInt (args[2]);
signalfilename = new String [args.length-3];
System.arraycopy (args, 3, signalfilename, 0, args.length-3);
// Process 0 executes the master section and the worker section in
// parallel threads.
if (rank == 0)
{
new ParallelTeam(2).execute (new ParallelRegion()
{
public void run() throws Exception
{
execute (new ParallelSection()
{
public void run() throws Exception
{
masterSection();
}
},
new ParallelSection()
{
public void run() throws Exception
{
workerSection();
}
});
}
});
}
// Processes 1 and up execute just the worker section.
else
{
workerSection();
}
// Stop timing.
long t2 = System.currentTimeMillis();
System.out.println ((t2-t1)+" msec "+rank);
}
// Hidden operations.
/**
* Execute the master section.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
private static void masterSection()
throws IOException
{
int worker;
PixelChunk chunk;
// Set up array of writers for output tissues data sets.
TissuesDataSetWriter[] writer =
new TissuesDataSetWriter [signalfilename.length];
for (int f = 0; f < writer.length; ++ f)
{
SignalDataSetReader reader =
new SignalDataSetReader (new File (signalfilename[f]));
int H = reader.getHeight();
int W = reader.getWidth();
reader.close();
writer[f] =
new TissuesDataSetWriter
(new File
(Files.fileNamePrepend (signalfilename[f], "tissues_")),
H, W);
}
// Set up schedule to analyze pixels in chunks of 100.
PixelSchedule schedule = new PixelSchedule (100, signalfilename);
// Send initial chunk to each worker. If null, no more work for that
// worker. Keep count of active workers.
int activeWorkers = size;
for (worker = 0; worker < size; ++ worker)
{
chunk = schedule.next();
world.send (worker, ObjectBuf.buffer (chunk));
if (chunk == null) -- activeWorkers;
}
// Repeat until all workers have finished.
while (activeWorkers > 0)
{
// Receive a chunk of pixel tissues from any worker.
ObjectItemBuf<PixelTissues[]> buf = ObjectBuf.buffer();
CommStatus status = world.receive (null, buf);
worker = status.fromRank;
// Send next chunk to that specific worker. If null, no more work.
chunk = schedule.next();
world.send (worker, ObjectBuf.buffer (chunk));
if (chunk == null) -- activeWorkers;
// Record pixel tissues in output tissues data set.
for (PixelTissues tissues : buf.item)
{
if (tissues != null)
{
writer[tissues.fileIndex()].addPixelTissues (tissues);
}
}
}
// All done.
for (int f = 0; f < writer.length; ++ f)
{
writer[f].close();
}
}
/**
* Execute the worker section.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
private static void workerSection()
throws IOException
{
int fileIndex = -1;
SignalDataSetReader reader = null;
Series t_series = null;
int M = 0;
double[][] A = null;
// Compute spin relaxation rates.
double[] R1 = new double [N+1];
double log_R1_lower = Math.log (R1_lower);
double log_R1_upper = Math.log (R1_upper);
double interval = (log_R1_upper - log_R1_lower)/N;
for (int j = 0; j <= N; ++ j)
{
R1[j] = Math.exp (log_R1_lower + j*interval);
}
ArraySeries R1_series = new ArraySeries (R1);
// Set up lists to receive analysis results.
ArrayList<Double> rho_list = new ArrayList<Double>();
ArrayList<Double> R1_list = new ArrayList<Double>();
// Repeat until no more work.
workerloop: for (;;)
{
// Receive a chunk of pixel indexes from the master. If null, no
// more work.
ObjectItemBuf<PixelChunk> buf = ObjectBuf.buffer();
world.receive (0, buf);
PixelChunk chunk = buf.item;
if (chunk == null) break workerloop;
int f = chunk.fileIndex();
int lb = chunk.pixelIndex();
int len = chunk.pixelCount();
// If we are now working on a different file:
if (f != fileIndex)
{
// Close old file.
if (reader != null) reader.close();
// Open new file.
fileIndex = f;
reader = new SignalDataSetReader (new File (signalfilename[f]));
// Get time series.
t_series = reader.getTimeSeries();
M = t_series.length();
// Compute design matrix.
A = new double [M] [N+1];
for (int i = 0; i < M; ++ i)
{
double[] A_i = A[i];
double t_i = t_series.x(i);
for (int j = 0; j <= N; ++ j)
{
A_i[j] = SpinSignal.S (R1[j], t_i);
}
}
}
// Set up array of pixel tissues to hold analysis results.
PixelTissues[] tissues = new PixelTissues [len];
// Process all pixels in chunk.
for (int i = 0; i < len; ++ i)
{
int index = lb + i;
PixelSignal signal_i = reader.getPixelSignal (index);
if (signal_i != null)
{
// Get measured spin signal.
Series S_series = signal_i.S_measured();
// Do the spin relaxometry analysis.
PixelAnalysis.analyze
(t_series, S_series, R1_series, A, rho_list, R1_list);
// Record analysis results.
tissues[i] = new PixelTissues (f, index, rho_list, R1_list);
}
}
// Send chunk of pixel tissues to the master.
world.send (0, ObjectBuf.objectBuffer (tissues));
}
// All done.
if (reader != null) reader.close();
}
/**
* Print a usage message and exit.
*/
private static void usage()
{
System.err.println ("Usage: java -Dpj.np=<K> edu.rit.mri.SpinRelaxometryClu <R1_lower> <R1_upper> <N> <signalfile> [<signalfile> ...]");
System.err.println ("<K> = Number of parallel processes");
System.err.println ("<R1_lower> = Lower R1 spin-lattice relaxation rate (1/sec)");
System.err.println ("<R1_upper> = Upper R1 spin-lattice relaxation rate (1/sec)");
System.err.println ("<N> = Number of R1 intervals");
System.err.println ("<signalfile> = Input spin signal data set file");
System.exit (1);
}
}
|