PDA

View Full Version : Tip: Passing parameters to Cn program with csrun


Tanner
08-26-2008, 10:33 AM
You don't need to write your own host code to pass simple parameters to your Cn program. You can use the -a option with csrun to pass strings to the argc/argv parameters of the main() function in the CSX program. For example:

Pass the numbers 4 8 and 12 to the CSX program, which will receive them as a strings pointed to by argv[1], argv[2] and argv[3]:
csrun example.csx -a "4 8 12"
or
csrun example.csx -a 4 -a 8 -a 12

The Cn program can convert these strings to integers using atoi, declared in stdlib.h

mikeyuan
10-28-2008, 02:21 AM
Hello,
I defined a dynamic array and used your way to pass the size of the array, the program runs but results are not correct. I input 96, it should return max=96, min=0, however, the output is:
-bash-3.00$ cscn -o test1.csx -lcn_reduction asc.cn countcycle_intinputargc.cn
"countcycle_intinputargc.cn" line 39: Warning: 'input_data' uninitialized
-bash-3.00$ csrun test1.csx -a 96
The cycles of transfer int are: 3144
The cycles of max_int for int are: 6300
The maximum element of the array is: 1111228416
The cycles of min_int for int are: 6401
The minimum element of the array is: 144
The cycles of reduce_cs_max for int are: 435
The maximum element of reduce_cs_max is: 1111228416
The cycles of reduce_cs_min for int are: 402
The minimum element of reduce_cs_min is: 144

I attached my code. Could you please check where is wrong?

Thanks,

Mike

Dangermouse
10-28-2008, 09:43 AM
You are not using argc/argv correctly, argc is the ARGument Count, i.e. the number of command line arguments which in the case of your example is two (and not 96 as you intended). You should use argc to determine how many elements are in the argv array, and you will need to parse this array. This is standard C so you can find plenty of examples on the internet, for instance take a look at this example (http://www2.its.strath.ac.uk/courses/c/subsection3_4_5.html) which takes an argument and uses it as an integer. You could also use atoi().

More importantly you are dereferencing an uninitialised pointer input_data. The compiler warned you about this:
"countcycle_intinputargc.cn" line 39: Warning: 'input_data' uninitialized
You must allocate memory for the array. If you are intending to add host code at a later date then I suggest you create some global variables for now and remember to remove them later on when you add the host code. Again, this is standard C.

mikeyuan
10-28-2008, 07:28 PM
I attached my codes, I used atoi() because I do not know how to implement scanf() in cn programs, could you please tell me how to do it because I do not want to use atoi? And I set a global variable 20000 to initialize the size of array input_data, no such warning then, do you think that I am doing correctly? Otherwise could you please tell me the best solution?
And the biggest problem that I have is, the min is always the max-96, which is wrong, should be 0, e.g,
-bash-3.00$ csrun test1.csx -a 200
The cycles of transfer int are: 9428
The cycles of max_int for int are: 6306
The maximum element of the array is: 199
The cycles of min_int for int are: 6428
The minimum element of the array is: 104
The cycles of reduce_cs_max for int are: 426
The maximum element of reduce_cs_max is: 199
The cycles of reduce_cs_min for int are: 417
The minimum element of reduce_cs_min is: 104

Could you please spare some time to check where I am wrong and how to fix it?

Thanks a lot,

Tanner
10-29-2008, 01:57 PM
Hi Mike,

The csapi_shared_mem example (typically installed to /opt/clearspeed/examples/csapi/shared_memory/) shows how to take a single argument and treat it as an integer. The important lines are:

#include <stdlib.h>
if( argc > 1 ) {
shared_mem_length = strtol(argv[1], NULL, 0);
}

Checking that argc (the argument count) is greater than 1 ensures there is at least one additional parameter (the CSX filename is always the first parameter).
The first additional parameter (argv[1]) is then converted from a string to an integer using strtol()

I recommend you print the result of the conversion to satisfy yourself that the argument is being received and converted correctly.

Tim

Tanner
11-03-2008, 12:01 PM
I've moved the rest of this thread to a new thread in General Programming since it was wandering off topic