How nested 'for' loops are inserting newlines in this program automatically? - for-loop

#include <stdio.h>
int main()
{
int alpha,numeric;
for(alpha='A';alpha<'K';alpha++)
{
for(numeric=0;numeric<10;numeric++)
{
printf("%c%d\t",alpha,numeric);
}
}
return 0;
}
The output I got is as follows.
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9
B0 B1 B2 B3 B4 B5 B6 B7 B8 B9
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
G0 G1 G2 G3 G4 G5 G6 G7 G8 G9
H0 H1 H2 H3 H4 H5 H6 H7 H8 H9
I0 I1 I2 I3 I4 I5 I6 I7 I8 I9
J0 J1 J2 J3 J4 J5 J6 J7 J8 J9
But, more neat and in form of a matrix, without any space between lines.
Someone please tell me how the printing is entering to new line even though there is no newline(\n) character in the program.

Related

Shuffle elements from three vectors using AVX

After doing few operations I got following three intermediate vectors.
__m256 Vec1 = [a0 a1 a2 a3 a4 a5 a6 a7]; //8 float values
__m256 Vec2 = [b0 b1 b2 b3 b4 b5 b6 b7]; //8 float values
__m256 Vec3 = [c0 c1 c2 c3 c4 c5 c6 c7]; //8 float values
I should rearrange these vectors as shown below for further processing.
__m256 ReVec1 = [a0 a1 b0 b1 c0 c1 a2 a3];
__m256 ReVec2 = [b2 b3 c2 c3 a4 a5 b4 b5];
__m256 ReVec3 = [c4 c5 a6 a7 b6 b7 c6 c7];
How can I shuffle three Vectors in AVX?

Ruby Program to print pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a hash and print that in the following Pattern :
Input 1 :
{
a: [1,2,3,4,5,6,7,8,9],
b: [1,2,3,4,5,6],
c: [2,3,4,5,6,7]
}
Output 1 :
A1 A2 A3 A4 A5 A6 A7 A8 A9
B1 B2 B3 B4 B5 B6
C2 C3 C4 C5 C6 C7
Input 2 :
{
a: [1,2,3,4,5,6,7],
b: [2,3,4,5,6],
c: [1,2,3,4,5,6,7]
}
Output 2 :
A1 A2 A3 A4 A5 A6 A7
B2 B3 B4 B5 B6
C1 C2 C3 C4 C5 C6 C7
For e.g.
If we consider Input 1 then the expectation is, the solution should add blank spaces at the missing number position.
=> It should return :
[
["A1","A2","A3","A4","A5","A6","A7","A8","A9"],
["B1","B2","B3","B4","B5","B6","","",""],
["","C2","C3","C4","C5","C6","C7","",""]
]
You can use collection, map or each for this
data = {a: [1,2,3,4,5,6,7,8,9],b: [1,2,3,4,5,6],c: [2,3,4,5,6,7]}
data.map{|k,v| (1..9).map{|a| data[k].include?(a) ? k.to_s.upcase() +a.to_s : ' '}}

MPI Gathering columns into bigger matrix

Lets say I 3 different matrix on 3 different threads:
a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
c1 c2 c3 c4 c5 c6
d1 d2 d3 d4 d5 d6
e1 e2 e3 e4 e5 e6
f1 f2 f3 f4 f5 f6
I want to gather them by using MPI_Gather into this one matrix
a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
c1 c2 c3 c4 c5 c6
d1 d2 d3 d4 d5 d6
e1 e2 e3 e4 e5 e6
f1 f2 f3 f4 f5 f6
I am using
MPI_Gather(&oldMatrix,N/size,columnType,&newMatrix,N/size,gatherColType,0,MPI_COMM_WORLD);
Where coltype is:
MPI_Type_vector(N,
1,
N/size,
MPI_FLOAT,
&column);
MPI_Type_commit(&column);
MPI_Type_create_resized(column, 0, 1*sizeof(float), &columnType);
MPI_Type_commit(&columnType);
However, I am not sure how should i create gatherColType (if it is needed). Can you help me about that?
note: N is matrix size and size is thread count (i.e. 3 in this question)

How can I modify this graph?

I have written this code:
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
It produces the following graph:
Actually I have three questions:
1.How can I make the edge A8 -> A3 neater? It looks very bad.
2.How can I make the edges A254 -> A8 + A8 -> A3 rectangular?
3.How can I make this graph vertical?
EDIT:
It is essential that the nodes are lined up and in the same order shown in the graph above.
This
is generated from
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A8,A3}
}
The best thing to do is read the documentation and experiment with the ideas in there
I have found the solution, here it is
You can make the edge neater by making a suitable adjustments to the distance between nodes and to the size of the nodes. And the most important thing is to force the A8->A3 edge to be longer by setting the minlen attribute of the corrosponding edge.
The following code
digraph G {
nodesep=0.5
node[fixedsize=true, shape="circle", width=0.5]
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [minlen = 3, constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
generates the following graph:
Edges can not be rectangular.
You can make the graph vertical by using the command rotate=90

golang: index efficiency of array

It's a simple program.
test environment: debian 8, go 1.4.2
union.go:
package main
import "fmt"
type A struct {
t int32
u int64
}
func test() (total int64) {
a := [...]A{{1, 100}, {2, 3}}
for i := 0; i < 5000000000; i++ {
p := &a[i%2]
total += p.u
}
return
}
func main() {
total := test()
fmt.Println(total)
}
union.c:
#include <stdio.h>
struct A {
int t;
long u;
};
long test()
{
struct A a[2];
a[0].t = 1;
a[0].u = 100;
a[1].t = 2;
a[1].u = 3;
long total = 0;
long i;
for (i = 0; i < 5000000000; i++) {
struct A* p = &a[i % 2];
total += p->u;
}
return total;
}
int main()
{
long total = test();
printf("%ld\n", total);
}
result compare:
go:
257500000000
real 0m9.167s
user 0m9.196s
sys 0m0.012s
C:
257500000000
real 0m3.585s
user 0m3.560s
sys 0m0.008s
It seems that the go compiles lot of weird assembly codes (you could use objdump -D to check it).
For example, why movabs $0x12a05f200,%rbp appears twice?
400c60: 31 c0 xor %eax,%eax
400c62: 48 bd 00 f2 05 2a 01 movabs $0x12a05f200,%rbp
400c69: 00 00 00
400c6c: 48 39 e8 cmp %rbp,%rax
400c6f: 7d 46 jge 400cb7 <main.test+0xb7>
400c71: 48 89 c1 mov %rax,%rcx
400c74: 48 c1 f9 3f sar $0x3f,%rcx
400c78: 48 89 c3 mov %rax,%rbx
400c7b: 48 29 cb sub %rcx,%rbx
400c7e: 48 83 e3 01 and $0x1,%rbx
400c82: 48 01 cb add %rcx,%rbx
400c85: 48 8d 2c 24 lea (%rsp),%rbp
400c89: 48 83 fb 02 cmp $0x2,%rbx
400c8d: 73 2d jae 400cbc <main.test+0xbc>
400c8f: 48 6b db 10 imul $0x10,%rbx,%rbx
400c93: 48 01 dd add %rbx,%rbp
400c96: 48 8b 5d 08 mov 0x8(%rbp),%rbx
400c9a: 48 01 f3 add %rsi,%rbx
400c9d: 48 89 de mov %rbx,%rsi
400ca0: 48 89 5c 24 28 mov %rbx,0x28(%rsp)
400ca5: 48 ff c0 inc %rax
400ca8: 48 bd 00 f2 05 2a 01 movabs $0x12a05f200,%rbp
400caf: 00 00 00
400cb2: 48 39 e8 cmp %rbp,%rax
400cb5: 7c ba jl 400c71 <main.test+0x71>
400cb7: 48 83 c4 20 add $0x20,%rsp
400cbb: c3 retq
400cbc: e8 6f e0 00 00 callq 40ed30 <runtime.panicindex>
400cc1: 0f 0b ud2
...
while the C assembly is more clean:
0000000000400570 <test>:
400570: 48 c7 44 24 e0 64 00 movq $0x64,-0x20(%rsp)
400577: 00 00
400579: 48 c7 44 24 f0 03 00 movq $0x3,-0x10(%rsp)
400580: 00 00
400582: b9 64 00 00 00 mov $0x64,%ecx
400587: 31 d2 xor %edx,%edx
400589: 31 c0 xor %eax,%eax
40058b: 48 be 00 f2 05 2a 01 movabs $0x12a05f200,%rsi
400592: 00 00 00
400595: eb 18 jmp 4005af <test+0x3f>
400597: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
40059e: 00 00
4005a0: 48 89 d1 mov %rdx,%rcx
4005a3: 83 e1 01 and $0x1,%ecx
4005a6: 48 c1 e1 04 shl $0x4,%rcx
4005aa: 48 8b 4c 0c e0 mov -0x20(%rsp,%rcx,1),%rcx
4005af: 48 83 c2 01 add $0x1,%rdx
4005b3: 48 01 c8 add %rcx,%rax
4005b6: 48 39 f2 cmp %rsi,%rdx
4005b9: 75 e5 jne 4005a0 <test+0x30>
4005bb: f3 c3 repz retq
4005bd: 0f 1f 00 nopl (%rax)
Could somebody explain it? Thanks!
The main difference is the the array bounds checking. In the disassembly dump for the Go program, there is:
400c89: 48 83 fb 02 cmp $0x2,%rbx
400c8d: 73 2d jae 400cbc <main.test+0xbc>
...
400cbc: e8 6f e0 00 00 callq 40ed30 <runtime.panicindex>
400cc1: 0f 0b ud2
So if %rbx is greater than or equal to 2, then it jumps down to a call to runtime.panicindex. Given you're working with an array of size 2, that is clearly the bounds check. You could make the argument that the compiler should be smart enough to skip the bounds check in this particular case where the range of the index can be determined statically, but it seems that it isn't smart enough to do so yet.
While you're seeing a noticeable performance difference for this micro-benchmark, it might be worth considering whether this is actually representative of your actual code. If you're doing other stuff in your loop, the difference is likely to be less noticeable.
And while bounds checking does have a cost, in many cases it is better than the alternative of the program continuing with undefined behaviour.

Resources