Starting from:
$35

$29

Lab 6: Programming with Chapel (Part 1) Solution

Download and unzip the le lab6.zip from D2L. You'll see a lab6 directory with some program les.







Hello World



Three simple versions of the Hello-World program are in les, hello[123].chpl. Read, then compile and run these programs:



linux chpl -o hello1 hello1.chpl linux ./hello1 -nl 1




Hello, world!




...




Note that to run a chapel program, you need to include the switch \-nl 1". This is because on our Linux system, the Chapel compiler is built to generate target code that is suitable for running across multiple locales. The switch \-nl 1" means running the program on a single locale.




You could also use the Makefile to compile these programs all together:




linux make hello1 hello2 hello3 linux ./hello1 -nl 1




Hello, world!




...




A forth version, hello4.chpl, contains a con gurable variable, message. Read, then compile and run the program. Try changing the message through the command-line a couple of times:



linux make hello4 linux ./hello4 -nl 1 Hello, world!




linux ./hello -nl 1 --message="Hi!" Hi!




The pair of les, hello.chpl and hello-main.chpl, contain a split version: one le de nes a module, the other uses it. Again, read, then compile and run the program.



Finally, there are three task-based versions of the Hello-World program: task[123].chpl. Run these programs and see if you obverse concurrency.






Domains and Arrays



The les, domain1.chpl and domain2.chpl, contains some domain-related code. Read the code carefully; pay attention to the connection between array a and domain D. Compile and run the pro-grams; notice how the changes on domain D a ect array a.



Change the program whatever way you want and see the results.




The le mmul.c contains a matrix multiplication program in C. Create a Chapel version of this program. Parallelizing as much as you can.


3. Deposit/Withdraw Program




The le bank.c is the same as you saw in Lab 5; it contains a sequential program performing simple deposit and withdraw operations on a bank account. Convert this program into two versions of Chapel program.




A sequential version, bank1.chpl. The ve constants should be converted to con gurable runtime constants, i.e. config const. To generate a random number, use the Random module. A sample is given below:



use Random;




var rs = new RandomStream(uint); // create a random stream of unsigned int




var val = rs.getNext(); // get a random unsigned int




The C formatted print statement printf can be converted to writef in Chapel almost without change, except that the integer control string is "%i" instead of "%d".




A parallel version, bank2.chpl. Convert both C for loops into Chapel forall loops, and make further changes so that deposit and withdraw operations can interleave.



Compile and run your Chapel program to verify that they work as expected.







































































































































2

More products