How can I find the id of a gang in OpenACC? - openacc

In OpenMP I can use omp_get_thread_num() to get the numerical id of the thread executing the code.
Is there a similar function I can use in OpenACC to get id of the gang executing a piece of code?

The OpenACC standard does not yet include such a function, but, with the PGI compiler, you can use the compiler extension function __pgi_gangidx() as follows:
//pgc++ -fast -acc -ta=tesla,cc60 -Minfo=accel test.cpp
#include <iostream>
#include "openacc.h"
int main(){
int gangs = 100;
int *ids = new int[gangs];
//Ensure everything is zeroed
for(int i=0;i<gangs;i++)
ids[i] = 0;
#pragma acc parallel num_gangs(gangs) copyout(ids[0:gangs])
{
ids[__pgi_gangidx()] = __pgi_gangidx();
}
for(int i=0;i<gangs;i++)
std::cout<<ids[i]<<" ";
std::cout<<std::endl;
}
Compile with:
pgc++ -fast -acc -ta=tesla,cc60 -Minfo=accel test.cpp
This gives as output:
0 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
as expected.
A suite of additional functions are available:
extern int __pgi_gangidx(void);
extern int __pgi_workeridx(void);
extern int __pgi_vectoridx(void);
extern int __pgi_blockidx(int);
extern int __pgi_threadidx(int);
Note that omp_get_thread_num() does not (yet?) work for GPU-targeted code.

Related

Ranking of nodes provides unranked result

I have the following graphviz input:
digraph para
{
node [shape=record];
rankdir=BT;
0 [label=<0<SUB>0,0</SUB>>];
1 [label=<1<SUB>1,1</SUB>>];
2 [label=<2<SUB>0,2</SUB>>];
3 [label=<2<SUB>1,1</SUB>>];
4 [label=<2<SUB>2,0</SUB>>];
5 [label=<3<SUB>1,3</SUB>>];
6 [label=<3<SUB>2,2</SUB>>];
7 [label=<3<SUB>3,1</SUB>>];
8 [label=<4<SUB>0,4</SUB>>];
9 [label=<4<SUB>1,3</SUB>>];
10 [label=<4<SUB>2,2</SUB>>];
11 [label=<4<SUB>3,1</SUB>>];
12 [label=<4<SUB>4,0</SUB>>];
13 [label=<5<SUB>1,5</SUB>>];
14 [label=<5<SUB>2,4</SUB>>];
15 [label=<5<SUB>3,3</SUB>>];
16 [label=<5<SUB>4,2</SUB>>];
17 [label=<5<SUB>5,1</SUB>>];
18 [label=<6<SUB>0,6</SUB>>];
19 [label=<6<SUB>1,5</SUB>>];
20 [label=<6<SUB>2,4</SUB>>];
21 [label=<6<SUB>3,3</SUB>>];
22 [label=<6<SUB>4,2</SUB>>];
23 [label=<6<SUB>5,1</SUB>>];
24 [label=<6<SUB>6,0</SUB>>];
25 [label=<7<SUB>1,7</SUB>>];
26 [label=<7<SUB>2,6</SUB>>];
27 [label=<7<SUB>3,5</SUB>>];
28 [label=<7<SUB>4,4</SUB>>];
29 [label=<7<SUB>5,3</SUB>>];
30 [label=<7<SUB>6,2</SUB>>];
31 [label=<7<SUB>7,1</SUB>>];
32 [label=<8<SUB>0,8</SUB>>];
33 [label=<8<SUB>1,7</SUB>>];
34 [label=<8<SUB>2,6</SUB>>];
35 [label=<8<SUB>3,5</SUB>>];
36 [label=<8<SUB>4,4</SUB>>];
37 [label=<8<SUB>5,3</SUB>>];
38 [label=<8<SUB>6,2</SUB>>];
39 [label=<8<SUB>7,1</SUB>>];
40 [label=<10<SUB>1,9</SUB>>];
41 [label=<7<SUB>7,1</SUB>>];
42 [label=<8<SUB>8,0</SUB>>];
43 [label=<5<SUB>4,1</SUB>>];
44 [label=<8<SUB>3,6</SUB>>];
45 [label=<8<SUB>5,4</SUB>>];
46 [label=<8<SUB>7,2</SUB>>];
47 [label=<1<SUB>1,1</SUB>>];
48 [label=<2<SUB>2,0</SUB>>];
49 [label=<3<SUB>1,3</SUB>>];
50 [label=<3<SUB>2,2</SUB>>];
51 [label=<3<SUB>3,1</SUB>>];
52 [label=<4<SUB>4,0</SUB>>];
53 [label=<6<SUB>1,5</SUB>>];
54 [label=<6<SUB>2,4</SUB>>];
55 [label=<7<SUB>3,5</SUB>>];
56 [label=<7<SUB>6,2</SUB>>];
57 [label=<7<SUB>7,1</SUB>>];
58 [label=<8<SUB>0,8</SUB>>];
59 [label=<8<SUB>3,5</SUB>>];
60 [label=<9<SUB>4,6</SUB>>];
61 [label=<1<SUB>0,1</SUB>>];
62 [label=<4<SUB>1,4</SUB>>];
63 [label=<5<SUB>2,3</SUB>>];
64 [label=<6<SUB>2,5</SUB>>];
65 [label=<6<SUB>6,1</SUB>>];
66 [label=<7<SUB>2,5</SUB>>];
67 [label=<7<SUB>3,4</SUB>>];
68 [label=<7<SUB>6,1</SUB>>];
69 [label=<7<SUB>7,0</SUB>>];
70 [label=<8<SUB>3,6</SUB>>];
71 [label=<8<SUB>4,5</SUB>>];
72 [label=<9<SUB>3,6</SUB>>];
73 [label=<5<SUB>1,5</SUB>>];
74 [label=<5<SUB>3,3</SUB>>];
75 [label=<6<SUB>4,2</SUB>>];
76 [label=<6<SUB>5,1</SUB>>];
77 [label=<7<SUB>4,4</SUB>>];
78 [label=<8<SUB>0,8</SUB>>];
{rank=same 0 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}
{rank=same 41 42}
{rank=same 43 44 45 46}
{rank=same 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{rank=same 61 62 63 64 65 66 67 68 69 70 71 72}
{rank=same 73 74 75 76 77 78}
4->5;
10->13;
24->29;
17->22;
12->15;
25->20;
39->46;
30->76;
36->59;
4->47;
27->58;
27->44;
29->75;
37->56;
30->71;
18->43;
7->50;
35->78;
8->49;
38->57;
21->54;
20->53;
9->50;
6->49;
4->61;
14->43;
24->41;
29->70;
10->51;
20->73;
15->52;
31->69;
31->69;
35->70;
22->74;
16->64;
23->67;
29->45;
33->67;
30->68;
24->65;
12->63;
5->48;
40->60;
9->62;
25->54;
4->49;
40->72;
37->60;
22->55;
18->63;
24->76;
34->59;
3->48;
0->47;
19->54;
32->55;
27->67;
37->72;
31->46;
22->66;
5->50;
32->66;
2->49;
29->77;
0->61;
26->55;
18->53;
35->77;
15->54;
11->52;
28->59;
1->48;
29->56;
37->69;
31->42;
18->73;
30->57;
20->74;
25->58;
25->44;
8->43;
18->64;
4->51;
13->52;
34->71;
27->75;
27->78;
28->76;
}
where I am trying to rank the nodes as follows:
{rank=same 0 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}
{rank=same 41 42}
{rank=same 43 44 45 46}
{rank=same 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{rank=same 61 62 63 64 65 66 67 68 69 70 71 72}
{rank=same 73 74 75 76 77 78}
However, it seems to me that only the first rank is assigned properly (for nodes 0,1,2,...,40). Could you please help me what the problem is with the ranking of the other groups? Why won't they be separated from each other?
rank=same can only force same rank for those nodes listed together, there is no guarantee that two rank=same gets different rank.
Also it seems like the hierarchy in you graph does not goes well with chosen subgraphs
Try replace your ranking with clusters and/or node coloring to analyze where the problem is. e.g.:
subgraph cluster1 {0 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}
subgraph cluster2 {41 42}
subgraph cluster3 {43 44 45 46}
subgraph cluster4 {47 48 49 50 51 52 53 54 55 56 57 58 59 60}
subgraph cluster5 {61 62 63 64 65 66 67 68 69 70 71 72}
subgraph cluster6 {73 74 75 76 77 78}
or
{0 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}
node[style=filled]
{node[fillcolor=red] 41 42}
{node[fillcolor=blue] 43 44 45 46}
{node[fillcolor=salmon] 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{node[fillcolor=pink] 61 62 63 64 65 66 67 68 69 70 71 72}
{node[fillcolor=green] 73 74 75 76 77 78}

GNU make file foreach loop is not working

In the GNU make file, need to have run_options30=Pkts=30. but it's not working.
list = 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
$(foreach var, $(list),$(eval run_options$(var)=Pkts=$(var) ) )

Is it possible to do a bash brace expansion using a variable bound without using eval?

Consider the following script.
#!/bin/bash
echo {00..99}
n=99
echo {00..$n}
The output of this script is:
00 01 02 03 04 05 06 07 08 09 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
{00..99}
The desired output is:
00 01 02 03 04 05 06 07 08 09 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
00 01 02 03 04 05 06 07 08 09 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
One solution which produces the desired output is
eval echo {00..$n}
Unfortunately, this solution uses eval which I'd prefer to avoid if possible.
Does anyone know of a way to obtain the desired result using brace expansion but not eval?
From the bash manual:
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.
Given that variable expansion comes after brace expansion, and that there is no way to induce a different order of operations without using eval, I would have to conclude that no, there is no way to avoid using eval.
Does anyone know of a way to obtain the desired without without eval?
You can use seq command,
seq -w -s ' ' 0 $n
Test:
sat $ seq -w -s " " 0 $n
00 01 02 03 04 05 06 07 08 09 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
Not sure if this meets your requirements, as it doesn't use braces, but (with GNU seq at least) the following produces the desired output:
$ n=99
$ seq -f%02.0f -s' ' 00 "$n"
00 01 02 03 04 05 06 07 08 09 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
The "-f" option produces the zero-padding, and the "-d" uses spaces to separate, rather than newlines.

Undefined method '[]' nilLNilClass for Adding columns in multiarrays

I am trying to solve the Projecteuler #11 but I am running into an error when I'm trying to create a function to calculate the multiplication of every 4 numbers in a column. I am getting an error:
Project11.rb:59:in `sumvertical': undefined method `[]' for nil:NilClass (NoMeth
odError)
I feel like there is something I am easily overlooking here. I appreciate the help!
#project #11 http://projecteuler.net/problem=11
grid="08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
grid=grid.split()
grid=grid.collect {|s| s.to_i}
multiarray=[]
i = 0
e = 19
until e > 400
multiarray << grid[i..e]
i+= 20
e+= 20
end
def sumhorizontal(x) #checks sum of all horizontal 4 elements
sum = 0
x.each {|a|
i=0
e=3
while e < a.length
if a[i..e].inject(:*) > sum
sum = a[i..e].inject(:*)
i += 1
e += 1
else
i += 1
e += 1
end
end
}
return sum
end
def sumvertical(x)
sum = 0
i=0
e=0
while e < x.length #Will break once the end point is longer than the length of an array
until i > 20 #Checks the first column
if x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e] > sum #Error is here
sum = x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e]
i += 1
else
i += 1
end
end
e += 1 #once you are out of the until statement, it increases e by 1 to check the next column
i = 0 #resets i so it can go back to the zero
end
return sum
end
print sumvertical(multiarray)
The grid has 20 rows. Your loop is actually trying to reach all the way to a 24rd row; that's because it goes through 21 iterations (i starts at 0, and goes until it equals 21), and each iteration reaches 3 beyond the current value of i (when you call x[i+3]). When i is 17, your code will break, because x[i+3][e] is trying to index into the 21st row of x. i+3 is 20, but the highest available index is 19. So what happens is, x[20] returns nil, and then the [] method is called on nil, which generates your error.
Also, the standard library has a transpose method that you can call on your array. If you use it, you just need one method (sumhorizontal). You can get the column sums with sumhorizontal(multiarray.transpose).
One more thing... it looks like you're coming from a procedural language. Ruby has an extensive standard library and coding constructs that can save you a lot of time and keystrokes. There is typically no need to iterate with while loops and index variables in Ruby. sumhorizontal, for instance, can be written like this (it should really be called producthorizontal, though if you're trying to solve Project Euler #11:
def sumhorizontal(x)
x.map { |r| r.each_slice(4).map { |s| s.reduce(:*) }.max }.max
end
Good luck with the rest of your Ruby learning journey!

how to display ascii character code

I encountered this ascii's style ascii table.
Of course I can store it in a file ascii and use cat ascii to display it content.
But I want to make it behavior more like a command.
UPDATE
When I read cs:app I find that how I bother to restore it in a file and using other commands.
Just run man ascii
If your shell supports aliases, you can do:
alias ascii='cat ~/ascii'
Then just type ascii et voila!
If you're using bash, put the above line in your .bashrc to persist it across logins. Other shells have similar features.
Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 # 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r
3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s
4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t
5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u
6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v
7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w
8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x
9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y
10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z
11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {
12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \ 108 6C l 124 7C |
13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }
14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~
15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL

Resources