Corning Community College

CSCS2650 Computer Organization

Project: IMPLEMENTATIONS AND OPTIMIZATIONS - PRIME NUMBER COMPUTATION (pnc1)

To get the project:

lab46:~/src/comporg$ grabit comporg pnc1

Wrapper Script Format:

#!/bin/bash
##
##

interpreter ${0}.ext ${*}
exit 0

for example:

#! /bin/bash

python ${0}.py ${*}
exit 0

Pnc0 was so that we could review the baseline scripts.

Pnc1 is the same skeleton as the pnc0 directory.

(primeregbs recommended) - gets the job done in a reasonable amount of time.

Pick one of the five programs you did in pnc0 and implement it in AT LEAST 5 different programming languages (2 of the 5 must be in languages that you've never used before)

languages currently installed on lab46:

If a language is not installed:

lab46:~$ aptitude search THING

send package details to wedge (IRC perhaps?)

Put each of these in their own directory. Example: Javascript implementation in js/, python implementation in python/

Put your primeregbs or whichever program you picked, and put it in each gcc directory (gccO0, gccO1, etc.), compile with the Makefile in each of these directories

We deserve nice things: To copy primeregbs.c from gcc directory to other directories without a bunch of different “cp” commands
See man xargs for more info:

lab46:~/src/comporg/pnc1$ echo gccO0 gccO1 gccO2 gccO3 gccOs tcc itcc | xargs -n 1 cp gcc/primeregbs.c

You will still need to run the make command.

See how it runs with pncrun for each compiler.

Command Line Arguments:
Just a friendly reminder that when command line arguments are passed they are usually (I don't know of any cases when they aren't actually) passed as strings. Make sure to cast it from a string to whichever data type it is supposed to be.

Go

 upBound, err = strconv.Atoi(os.Args[1]) 

Lua

 local upBound = tonumber(arg[1])

Python3
I don't know for sure if this works for Python2

 upBound = int(sys.argv[1]) 

To compile a go program:

lab46:~/src/comporg/pnc1/go$ go build program_name.go

This will give you an executable that you can run in the typical way for pnc programs:

lab46:~/src/comporg/pnc1/go$ ./program_name QTY

A few tips for go: printing to stderr can be done like so:

import "fmt"
import "os"

fmt.Fprintf(os.Stderr, "You did something bad and now the program must yell at you\n")

To convert a string (such as your command line input) into a number that will prevent infinite looping later on:

import "strconv"
import "os"

var qty int   // this is how you declare an int variable called quantity
var err error // this is how you declare an error type variable called err

qty, err = strconv.Atoi(os.Args[1])

You need an error variable because this atoi function, like some things you see in python, returns two things… you can check if this error is nil or not (it should be nil if everything worked), which I recommend because if you don't use it the compiler will yell at you. Some examples of code will do something like

val, err := strconv.Atoi(a_string)

where the := is go syntax for declaring and initializing a variable simultaneously. When I did this, however, I'd get errors later on about qty not being defined (might have something to do with defining a variable inside an if block and then trying to use it outside that block, which is a thing with lua). So, the method above works if you want to use qty later on in the program (which you probably do).

Here's a reference for using strconv: https://golang.org/pkg/strconv/

And a reference for go in general, which I found very useful: https://gobyexample.com/

To run a lua program:

lab46:~/src/comporg/pnc1/lua$ lua5.3 program_name.lua QTY

One thing to watch out for with lua is that while it has an arg[0] value that's analogous to argv[0] in C and holds the program name, it doesn't count this when calculating the number of arguments provided. In a way, this makes it “smarter” than C, but watch out… if you want to check for a number of arguments, you can use #arg (which returns the number of command line arguments provided), but if you want one command line argument, it's #arg == 1, NOT #arg == 2. If you've got argument checking in place it'll start spitting out errors telling you you don't have enough arguments when you actually do.

To write to stderr in lua:

 io.stderr:write() 

Reference: https://www.lua.org/pil/21.2.html

Timing

Thanks to a barrage of stackexchange and other internet sources which have been lost in the many, many pages I've visited, I have figured out how to do timing in python and ruby.

To do timing in python:

import time

start = time.time()

# prime calculation code here

end = time.time()

total = end - start

sys.stdout.write("%8.4lf\n" % total)

To do it in ruby:

require "benchmark"

time = Benchmark.realtime do

// insert prime calculation code here

end

$stdout.printf "%8.4f\n" time

To do it in go:

import "time"

start := time.Now() // start time

// prime code here

end := time.Now() // end time

diff := end.Sub(start) // calculate elapsed time

fmt.Fprintf(os.Stdout, "%8.4f\n", diff.Seconds())

Reference: https://golang.org/pkg/time/

To do it in lua:

st_time = os.clock() -- start time

-- execute code here

end_time = os.clock() -- end time... note that end is verboten as a variable name in lua

io.write(string.format("%8.4f\n", end_time - st_time))

Code modeled after the first answer here: https://stackoverflow.com/questions/24015241/timing-a-function-tasks-in-lua?lq=1

To do it in Java:

long startTime = System.nanoTime();
.....your program....
long endTime   = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);

(You can change the output format as this will give you nano seconds) Or if you don't want to deal with nanoseconds you can use “System.currentTimeMillis()” instead

How to compile and run a java program in the terminal

Because compiling a java program will cause memory issues, some boundaries are needed to compile the program:

javac -J-XX:MaxHeapSize=512M -J-XX:InitialHeapSize=256M
-J-XX:CompressedClassSpaceSize=128M -J-XX:MaxMetaspaceSize=128m -J-XX:MetaspaceSize=64m primeregbs.java

To run the program:

export CLASSPATH=${HOME}/src/comporg/pnc1/java

java -XX:MaxHeapSize=512M -XX:InitialHeapSize=256M -XX:CompressedClassSpaceSize=128M -XX:MaxMetaspaceSize=128m -XX:MetaspaceSize=64m primeregbs ${*}

To display the time in Bash:

startTime=`date +%s%N`

-Pnc1 code-

endTime=`date +%s%N`

totalTime=`bc <<< "scale=5; ${endTime} - ${startTime}"`
totalTime=`bc <<< "scale=4; ${totalTime} / 1000000000"`
echo "$totalTime"

JavaScript:

The wrapper is as follows:

#!/bin/bash
##
## Wrapper for nodejs script
##
nodejs ${0}.js ${*}
exit 0

To do timing in JavaScript:

var start = new Date().getTime();


-Pnc1 code-

var end = new Date().getTime();

var time = (end-start)/1000;

console.log(time);