MPI: MPI_Get not working - mpi

The following code creates a window in process 0 (master) and the other processes put some values in it and I'm trying to get the window of the master from other processes each time to print it but I'm getting totally confusing results. Here's the code:
int main ( int argc, char *argv[] )
{
int id;
MPI_Init ( &argc, &argv );
MPI_Comm_rank ( MPI_COMM_WORLD, &id );
MPI_Win win;
if (id == 0)
{
int *arr;
int in = 0;
MPI_Alloc_mem(10 * sizeof(int), MPI_INFO_NULL, &arr);
for(in = 1; in < 10; in++)
{
arr[in] = -1;
}
arr[0] = 0;
MPI_Win_create(arr, 10 * sizeof(int), sizeof(int), MPI_INFO_NULL,MPI_COMM_WORLD, &win);
int ready = 0;
while (!ready)
{
MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
ready = fini(arr);
MPI_Win_unlock(0, win);
}
printf("All workers checked in using RMA\n");
MPI_Win_free(&win);
MPI_Free_mem(arr);
printf("Master done\n");
}
else
{
int one = id;
int *local;
int i = 0;
MPI_Alloc_mem(sizeof(int)*10, MPI_INFO_NULL, &local);
MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
for(i = 0; i < 10; i++)
{
MPI_Get(&local[i], 1, MPI_INT, 0, i,1, MPI_INT, win);
}
printf("Worker before %d: ", id);
pt(local); //Printing array "local"
MPI_Put(&one, 1, MPI_INT, 0, id, 1, MPI_INT, win);
for(i = 0; i < 10; i++)
{
MPI_Get(&local[i], 1, MPI_INT, 0, i,1, MPI_INT, win);
}
printf("Worker %d done: ", id);
pt(local);
MPI_Win_unlock(0, win);
MPI_Win_free(&win);
}
MPI_Finalize ( );
return 0;
}
int fini ( int table[] )
{
int i = 0;
while(i < 10)
{
if(table[i] == -1) return 0;
i++;
}
return 1;
}
void pt(int t[])
{
int i = 0;
while(i < 10)
{
printf("%d ",t[i]);
i++;
}
printf("\n");
}
This actually gives me the following result:
Worker before 4: 1152288776 32731 1152288776 32731 4 0 0 0 48 0
Worker 4 done: 1152288776 32731 1152288776 32731 4 0 0 0 48 0
Worker before 1: 1525372936 32743 1525372936 32743 4 0 0 0 48 0
Worker 1 done: 1525372936 32743 1525372936 32743 4 0 0 0 48 0
Worker before 3: 1422645256 32661 1422645256 32661 4 0 0 0 48 0
Worker 3 done: 1422645256 32661 1422645256 32661 4 0 0 0 48 0
Worker before 6: 1553508328 32675 34348016 0 0 0 33 0 1553508280 32675
Worker 6 done: 1553508328 32675 34348016 0 0 0 33 0 1553508280 32675
Worker before 5: -1745405976 32676 33995760 0 0 0 33 0 -1745406024 32676
Worker 5 done: -1745405976 32676 33995760 0 0 0 33 0 -1745406024 32676
Worker before 9: -990742552 32568 20737008 0 0 0 33 0 -990742600 32568
Worker 9 done: -990742552 32568 20737008 0 0 0 33 0 -990742600 32568
Worker before 2: 1455122440 32635 1455122440 32635 4 0 0 0 48 0
Worker 2 done: 1455122440 32635 1455122440 32635 4 0 0 0 48 0
Worker before 7: -1086933016 32650 18463728 0 0 0 33 0 -1086933064 32650
Worker 7 done: -1086933016 32650 18463728 0 0 0 33 0 -1086933064 32650
Worker before 8: 1328670696 32548 24464368 0 0 0 33 0 1328670648 32548
Worker 8 done: 1328670696 32548 24464368 0 0 0 33 0 1328670648 32548
Can you please help me figure out what's wrong with my code ? Thanks.
edit : Apparently the problem is that MPI_Get doesn't fill the "local" buffer...

Related

QML how to use a dynamic topmargin correctly

I want to enlarge or reduce a button depending on the presence of an icon. The buttons are in a rectangle and I would like to realize it with the top margin, because the buttons also have rounded corners and only the top corners should be visible.
The goal is a representation like this
If I set the topmargin fix to 10 it looks good
If I calculate the value depending on whether an icon is present, the buttons without icon are correctly displayed deeper, but the buttons with icon sit too deep
Like the picture before the topmargin for the icon buttons is 10 but they are moved lower.
Any idea - the code for display.qml is reduced and does not show all the properties. if they are needed i will add them.
FooterButton.qml
Rectangle {
id: button
property string p_identity
property string p_icon
property string p_source
property string p_backgroundColor
property int p_topmargin: 10
height: 70
width: 80
Layout.leftMargin: 25
Layout.topMargin: p_topmargin
color: p_backgroundColor
radius: 10
border.color: "black"
border.width: 0
Connections {
target: m_screen;
onScreenChanged: {
p_icon = m_screen.getButtonIcon(p_identity)
p_source = (!p_icon || p_icon.length === 0) ? "" : "image://iconprovider/" + p_icon)
// p_topmargin = 10
p_topmargin = (!p_icon || p_icon.length === 0) ? 45 : 10
}
}
Footer.qml
Item {
id: footer
property string p_footerBackgroundColor: "yellow" //m_config.getColor(Colors.FooterBackground)
property string p_buttonBackgroundColor: m_config.getColor(Colors.ButtonBackground)
Rectangle { anchors.fill: parent; x: footer.x; y: footer.y; width: footer.width; height: footer.height; color: p_footerBackgroundColor
RowLayout{ anchors.fill: parent
FooterButton{ p_identity: "FB1"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB2"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB3"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB4"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB5"; p_backgroundColor: p_buttonBackgroundColor }
}
}
Display.qml
Item {
id: display
Header { x: 0; y: 0; width: display.width; height: p_headerHeight; visible: p_headerVisible; color: p_backgroundColor; p_buttonColor: p_buttonBackgroundColor }
Left { x: 0; y: p_headerHeight; width: p_borderWidth; height: p_contentHeight; color: "blue" }
Right { x: display.width - p_encoderWidth; y: p_headerHeight; width: p_encoderWidth; height: p_contentHeight; p_color: "magenta" }
Footer { x: 0; y: display.height - p_footerHeight; width: display.width; height: p_footerHeight; visible: p_footerVisible }
just making sure you're not over-engineering your problem, but, I want to point out that the standard Button has both icon support and the ability to change the background to a rounded Rectangle:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
footer: Frame {
background: Rectangle {
color: "yellow"
}
RowLayout {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 20
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
icon.source: "hammer.svg"
}
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
icon.source: "check.svg"
}
}
}
}
// AppButton.qml
import QtQuick
import QtQuick.Controls
Button {
width: 100
height: 100
background: Rectangle {
color: pressed ? palette.mid : palette.button
radius: 20
}
icon.source: "blank.svg"
icon.width: 64
icon.height: 64
}
// blank.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
</svg>
// hammer.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M29.64 25.462c-1.186-1.62-3.535-4.176-6.254-7.136-2.657-2.893-5.671-6.173-8.066-9.11a3.883 3.883 0 0 1-1.044-1.531 6.899 6.899 0 0 0-.215-1.271 3.427 3.427 0 0 1-.08-.348 7.985 7.985 0 0 1 3.153-1.61 25.43 25.43 0 0 1 4.095-.527l1.08-.043-1.14-1.239-.148-.035a24.293 24.293 0 0 0-5.123-.606A13.096 13.096 0 0 0 7.53 4.82c-.225.2-1.433 1.478-1.338 2.334.078.73-.212.949-.792 1.383a8.35 8.35 0 0 0-.558.444c-1.468-.125-1.92.252-3.014 1.16l-.39.32-.095.105a1.472 1.472 0 0 0-.277 1.24 7.214 7.214 0 0 0 2.294 3.029 2.25 2.25 0 0 0 2.404-.483 18.003 18.003 0 0 0 1.577-2.018 2.67 2.67 0 0 1 1.633-1.26 12.815 12.815 0 0 1 2.588.88c.11.046.2.077.277.104.05.018.111.032.116.036 4.108 5.004 6.896 8.936 8.93 11.807 1.401 1.976 2.413 3.404 3.3 4.412l.912 1.038a1.935 1.935 0 0 0 1.362.651l.078.001a1.939 1.939 0 0 0 1.334-.534l1.548-1.486a1.927 1.927 0 0 0 .22-2.52zM12.059 11.028l-.029.034c-.03-.012-.052-.018-.088-.033a10.285 10.285 0 0 0-3-.954 3.577 3.577 0 0 0-2.454 1.738 21.031 21.031 0 0 1-1.375 1.786c-.605.434-.936.519-1.313.338a6.931 6.931 0 0 1-1.792-2.446.85.85 0 0 1 .125-.305l.334-.275c1.045-.867 1.228-1.021 2.299-.933a1.02 1.02 0 0 0 .738-.247A7.72 7.72 0 0 1 6 9.337a2.27 2.27 0 0 0 1.186-2.288A3.785 3.785 0 0 1 8.19 5.571a12.232 12.232 0 0 1 7.706-2.565 20.9 20.9 0 0 1 2.624.178c-.523.076-1.076.173-1.614.298A9.024 9.024 0 0 0 13.34 5.3a1.176 1.176 0 0 0-.25 1.356 5.831 5.831 0 0 1 .19 1.1 3.345 3.345 0 0 0 .842 1.625 9.48 9.48 0 0 0-.994.683 7.036 7.036 0 0 0-1.068.964zm16.668 16.234l-1.547 1.485a.945.945 0 0 1-.678.256.924.924 0 0 1-.652-.312l-.912-1.038c-.853-.97-1.905-2.452-3.236-4.33-2.018-2.848-4.78-6.742-8.838-11.696a6.433 6.433 0 0 1 .875-.772 8.145 8.145 0 0 1 .964-.66l.09-.05C17.14 13 20.06 16.182 22.65 19.001c2.7 2.939 5.032 5.477 6.184 7.051a.923.923 0 0 1-.106 1.209z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
// check.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M13.5 22.142L7.59 16.42l.636-.636L13.5 20.87 26.721 7.8l.637.637z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
You can Try it Online!

Draw this loop in Postscript

Any idea how to draw this in Postscript using for loop and ifelse conditional?
My idea was to make a large red circle, then a smaller white circle and a smaller red circle again...
Also we can see that the color is getting darker so it should also be saved as a variable that gets darker.
50 50 translate
/coordinate_system {0.5 0.3 0 0 setcmykcolor
gsave
2 setlinewidth 500 0 moveto 0 0 lineto 0 500 lineto stroke
grestore
gsave
0.3 setlinewidth
9 { 30 100 moveto 500 100 lineto stroke 0 50 translate } repeat
grestore
gsave
0.3 setlinewidth
10 { 100 20 moveto 100 500 lineto stroke 50 0 translate } repeat
grestore
gsave
/tekst 3 string def /Helvetica findfont 10 scalefont setfont
100 100 500 { /y exch def 5 y 2 sub moveto y tekst cvs show } for
90 100 500 { /x exch def x 5 moveto x 10 add tekst cvs show } for
grestore
0 setgray } bind def
/s { mark pstack pop } def
coordinate_system
And this is the code so far...
100 100 translate
%100 -3 0 {{1 0 0 setrgbcolor exch 0 exch 0 360 arc stroke}{0 0 0 setrgbcolor exch 0 exch 0 360 arc stroke} ifelse} for
3 4 lt {1 0 0 setrgbcolor 0 0 50 0 360 arc stroke}{0 0 0 setrgbcolor 0 0 100 0 360 arc stroke} ifelse
The following code loops for i = 1, 2, ..., 10. I am using i to control the radius and color of the circle.
/i 1 def
{
i 0.1 mul 0 0 setrgbcolor % RGB (i*0.1, 0, 0)
i 10 gt { exit } if % exit the loop if i > 10
300 300 % center at 300 300
20 i mul % radius 20*i
drawcircle
/i i 1 add def % i = i + 1
} loop
drawcircle code:
/drawcircle % XO YO R
{
newpath
0 360 arc
closepath
stroke
} bind def
3 setlinewidth
My output (cropped a bit) is:

What does IM0/1 mean in z80.info decoding?

I am in the process of writing (yet another) Z80 simulator.
I am using the decoding page on the z80.info site.
In the section with the lookup/disssambly tables it says that for index 1 and 5 the Interrupt Mode is IM0/1. This table is referred to from the IM instruction (ED) X=1, Z=6.
What does IM0/1 mean exactly?
I know it's not an official instruction but I am also trying to support undocumented instructions.
As found here, quoting from Gerton Lunter:
The instructions ED 4E and ED 6E are IM 0 equivalents: when FF was put on the bus
(physically) at interrupt time, the Spectrum continued to execute normally, whereas
when an EF (RST 28h) was put on the bus it crashed, just as it does in that case when
the Z80 is in the official interrupt mode 0. In IM 1 the Z80 just executes a RST 38h
(opcode FF) no matter what is on the bus.
So it pretty much means IM 0, and I'm not sure where the commonly seen /1 comes from.
IM0/1/2 are instructions setting the Z80 CPU int interrupt mode 0/1/2. Each mode handles the maskable interrupts differently. Its years I use those but IIRC:
IM0
executes opc placed on databus by external HW
IM1
calls fixed ISR at 38h
IM2
calls ISR from ISR entry point table which is placed where i register points
Here relevant interrupt C++ code extracted from my emulator:
//---------------------------------------------------------------------------
void Z80::_reset()
{
im=0;
iff1=0;
iff2=0;
reg.r16.pc =0x0000;
reg.r16.af =0xFFFF;
reg.r16.bc =0xFFFF;
reg.r16.de =0xFFFF;
reg.r16.hl =0xFFFF;
reg.r16.ix =0xFFFF;
reg.r16.iy =0xFFFF;
reg.r16.ir =0xFFFF;
reg.r16.sp =0xFFFF;
reg.r16._af=0xFFFF;
reg.r16._bc=0xFFFF;
reg.r16._de=0xFFFF;
reg.r16._hl=0xFFFF;
reg.r16.alu=0xFFFF;
reg.r16.mem=0xFFFF;
reg.r16.io =0xFFFF;
reg.r16.nn =0xFFFF;
time=0; time0=0; dtime=0;
busrq=false;
busack=false;
}
//---------------------------------------------------------------------------
void Z80::_int()
{
if (!_enable_int) return;
if (!iff1) return;
if (actual->ins==_z80_ins_HALT) reg.r16.pc+=actual->size;
if ((actual->ins==_z80_ins_EI)||(actual->ins==_z80_ins_DI)) execute();
iff1=0;
iff2=0;
if (im==0)
{
// execute instruction on databus db from peripherials
mc=0;
actual=&ins_int0;
time+=actual->mc[mc]; mc++; // fetch INT
BYTE db[4];
db[0]=db8;
db[1]=db8;
db[2]=db8;
db[3]=db8;
execute(db);
}
else if (im==1)
{
mc=0;
actual=&ins_int1;
time+=actual->mc[mc]; mc++; // fetch INT
_push(reg.r16.pc);
reg.r16.pc=0x0038; // fixed vector 38h
}
else if (im==2)
{
mc=0;
actual=&ins_int2;
time+=actual->mc[mc]; mc++; // fetch INT
_push(reg.r16.pc);
union { BYTE db[2]; WORD dw; } ubw;
ubw.db[1]=reg.r8.i; // H
ubw.db[0]=db8; // L
reg.r16.pc=_readw(ubw.dw); // vector from mem[i+db8]
}
}
//---------------------------------------------------------------------------
void Z80::_nmi()
{
if (actual->ins==_z80_ins_HALT) reg.r16.pc+=actual->size;
if ((actual->ins==_z80_ins_EI)||(actual->ins==_z80_ins_DI)) execute();
iff2=iff1; // iff2 ide do flagov po ld a,i alebo ld a,r
iff1=0;
mc=0;
actual=&ins_nmi;
time+=actual->mc[mc]; mc++; // fetch NMI
_push(reg.r16.pc);
reg.r16.pc=0x0066; // fixed vector 66h
}
//---------------------------------------------------------------------------
Here all the IM instructions in order extracted from here What's the proper implementation for hardware emulation?:
opc T0 T1 MC1 MC2 MC3 MC4 MC5 MC6 MC7 mnemonic
ED46 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM0
ED4E 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM0
ED56 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM1
ED5E 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM2
ED66 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM0
ED6E 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM0
ED76 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM1
ED7E 08 00 M1R 4 M1R 4 ... 0 ... 0 ... 0 ... 0 ... 0 IM2
As you can see its:
IM value: 0 0 1 2 0 0 1 2
And your linked page:
IM value: 0 0/1 1 2 0 0/1 1 2
so I expect it just mean how the opc is encoded but you right the tables there are not very obvious.
The IM0/1 are duplicates of IM0 so I am guessing they were not in original documentation and was discovered only latter ... without the exact behavior knowledge at that time your table was created... There are a lot of originally undocumented (secret) instructions so if your source of info does not contain them accurately maybe you should not use it and move to better docs to avoid problems and incompatibilities in future...

r if else for loop with logical and boollean operators

I'm trying to use an if else statement to create a new column of binary data in my data frame, but what I get is all zeros...
command:
for(i in 1:nrow(asort)){
if(asort$recip==0 && asort$dist<.74){
asort$temp[i]<-0
} else{
asort$temp[i]<-1
}
}
#temp ends up being all 0's
In addition, I would actually like to ask something along the lines of this:
# if the data in the recip column = 0, and the distances is < 0.74, OR if the #data is greater than 1.85 give me a zero, else 1
for(i in 1:nrow(asort)){
if(asort$recip==0 && asort$dist<.74 || asort$dist>1.85){
asort$temp[i]<-0
} else{
asort$temp[i]<-1
}
}
> head(asort)
coordinates CLASS_ID Flight UFID dist nnid nnid2 observed recip temp
157 (285293.3, 4426017) 0 F4_ F4_156 0.3857936 158 F4_157 0 0 0
158 (285293.2, 4426017) 0 F4_ F4_157 0.3857936 157 F4_156 0 0 0
259 (285255, 4426014) 0 F4_ F4_258 0.5226039 261 F4_260 1 0 0
261 (285255, 4426014) 0 F4_ F4_260 0.5226039 259 F4_258 1 0 0
402 (285275.3, 4426004) 0 F4_ F4_401 0.5598427 403 F4_402 1 0 0
403 (285275.6, 4426004) 0 F4_ F4_402 0.5598427 402 F4_401 1 0 0
Using df data.frame
dist <- runif(10, 0.3, 2)
recip<- c(0,1,1,0,1,0,1,0,0,1)
df <- data.frame(dist, recip)
and ifelse
df$temp<-ifelse(df$dist < 0.74 & df$recip == 0 , 0,
ifelse(df$dist > 1.85 & df$recip == 0, 0, 1))
> head(df)
# dist recip temp
#1 1.1878002 0 1
#2 0.4147835 1 1
#3 1.3707311 1 1
#4 0.9008034 0 1
#5 1.0220149 1 1
#6 1.9384069 0 0

Efficient (repeat) looping

I am trying to evaluate if a price, price(k), in a given row,(k), is equal to the one above, price(k-1). If it is I want to sum the volume from the prior and the price in question, volume(k)+volume(k+1), and then remove the row with the duplicate price, row k.
I have the following repeat loop which I am applying to a large dataset looking to delete repeated values.
k <- 1
repeat{
if( Prices$Price[ k + 1 ] == Prices$Price[ k ] ){
Prices$CumVolume[ k + 1 ] <- Prices$CumVolume[ k + 1 ] + Prices$CumVolume[ k ]
Prices <- Prices[ -k , ]
k <- k + 1
if( k > nrow( Prices ) ) break
}
}
The loop is very slow and I was wondering if there are ways to speed it up. Unfortunately I am relatively new to R and am having difficulty working out the best way to go about this.
Also is there a way in R to observe the iteration the loop is currently up too? i.e. have it displayed in the workspace on each iteration?
Example data:
Date Time Price CumVolume Ret MeanRet VolRet
26 01-JAN-2009 21:30:01.783 96.660 537 0 0 0
31 01-JAN-2009 21:30:58.041 96.650 78 0 0 0
33 01-JAN-2009 21:34:09.589 96.640 60 0 0 0
35 01-JAN-2009 21:34:10.879 96.640 40 0 0 0
37 01-JAN-2009 21:35:55.001 96.635 50 0 0 0
It appears you want something like this:
DF <- read.table(text=" Date Time Price CumVolume Ret MeanRet VolRet
26 01-JAN-2009 21:30:01.783 96.660 537 0 0 0
31 01-JAN-2009 21:30:58.041 96.650 78 0 0 0
33 01-JAN-2009 21:34:09.589 96.640 60 0 0 0
35 01-JAN-2009 21:34:10.879 96.640 40 0 0 0
37 01-JAN-2009 21:35:55.001 96.635 50 0 0 0", header=TRUE)
#create a run id
DF$runs <- cumsum(c(TRUE, diff(DF$Price) != 0))
#sum per each price run
DF$CCVolume <- with(DF, ave(CumVolume, runs, FUN=sum))
#remove duplicated prices
DF[!duplicated(DF$Price), ]
# Date Time Price CumVolume Ret MeanRet VolRet runs CCVolume
#26 01-JAN-2009 21:30:01.783 96.660 537 0 0 0 1 537
#31 01-JAN-2009 21:30:58.041 96.650 78 0 0 0 2 78
#33 01-JAN-2009 21:34:09.589 96.640 60 0 0 0 3 100
#37 01-JAN-2009 21:35:55.001 96.635 50 0 0 0 4 50
I think your code is going in infinite loop because of your increment index.K=k+1 and Break is always within the condition,I hope you want this
k=1
z=unique(Prices$Price)
for(i in 1:length(z))
{
dupindex=which(z[i]==Prices$Price)
Prices$CumVolume[tail(dupindex,n=1)]=sum(Prices$CumVolume[dupindex])
Prices=Prices[-(dupindex[1:length(dupindex)-1]),]
}
I hope it help,thanks.

Resources