MPI multiple sendrecv calls fortran - fortran

I am working on a finite volume code and want to make MPI work. Now I am struggling with sending corners from one domain to the other. With 8 ranks the domain is divided as follow in coordinates (not matrix representation):
|1|3|5|7|
----------
|0|2|4|6|
Each domain is a subarray whose elements are equal to its rank. For example, since I am solely focused on sending corners :
I am to send 1 from the process 1 to process 2 and
send 2 from the process 2 to process 1. The following code, only works for even (resp.odd) ranks if u comment the first (resp.last) two sendrecv. Which is normal. But if the four sendrecv are uncommented only the odd ranks will receive the values. The attended result is as follow :
5 | | 2 5 5 5 5
5 | | 5 5 5 5 5
5 | | 5 5 5 5 5
5 | | 6 5 5 5 5
6 | | 6 6 6 6 5
6 | | 6 6 6 6 6
6 | | 6 6 6 6 6
7 | | 4 7 7 7 7
7 | | 7 7 7 7 7
7 | | 7 7 7 7 7
0 | | 0 0 0 0 0
0 | | 0 0 0 0 0
0 | | 0 0 0 0 3
1 | | 1 1 1 1 1
1 | | 1 1 1 1 1
1 | | 2 1 1 1 1
2 | | 2 2 2 2 1
2 | | 2 2 2 2 2
2 | | 2 2 2 2 2
2 | | 2 2 2 2 5
3 | | 0 3 3 3 3
3 | | 3 3 3 3 3
3 | | 3 3 3 3 3
3 | | 4 3 3 3 3
4 | | 4 4 4 4 3
4 | | 4 4 4 4 4
4 | | 4 4 4 4 4
4 | | 4 4 4 4 7
The print is in matrix representation (so the equivalent in coordinates/real notation is to transpose then flip the matrix according to the horizontal axis).
The code :
program scatter
use mpi
implicit none
integer,parameter :: nrows = 8,ncols = 8
integer, dimension(8,8) :: global
integer, dimension(8) :: adj
integer, dimension(2) :: dims_orig,dims,coo,sizes,subsizes,coo_init
integer, dimension(2) :: coordsnw,coordsne,coordssw,coordsse
integer, dimension(2) :: size_glob, size_loc
logical,dimension(2) :: periods
integer, dimension(:,:),allocatable :: local
integer, dimension(:),allocatable :: displ,counts
integer :: info,code,rank,n_procs,comm2d
integer :: ndi,ndj,nni,nnj,nni_last,nnj_last
integer :: i1,i2,j1,j2,ii1,ii2,jj1,jj2,row,col
integer :: type0,type_subarray,sizeofint,i,j,ii,kk
integer :: imax,jmax,loc_size,ndd,tag=1
integer :: iminR,imaxR,jminR,jmaxR
integer :: type_corner,type_base,sizeofdouble
integer, dimension(mpi_status_size) :: stats
integer(kind=mpi_address_kind) :: start,extend
logical :: reorder
imax = nrows
jmax = ncols
global = -10
call mpi_init(code)
call mpi_comm_rank(mpi_comm_world,rank,code)
call mpi_comm_size(mpi_comm_world,n_procs,code)
dims_orig = 0
CALL MPI_DIMS_CREATE(n_procs ,2,dims_orig,code)
! define number of subdomain in each direction
ndi = dims_orig(1)
ndj = dims_orig(2)
! define size of subdomain in each direction
nni = nrows/ndi
nnj = ncols/ndj
nni_last = nrows-(ndi-1)*nrows/ndi
nnj_last = ncols-(ndj-1)*ncols/ndj
dims(1) = ndi
dims(2) = ndj
periods = .false.
reorder = .true.
! order the rank according to the domain separation
call mpi_cart_create(MPI_COMM_WORLD,2,dims,periods,reorder,comm2d,code)
! get rank in the new comm_world
call mpi_comm_rank(comm2d,rank,code)
! get coordinates (for example (1,0)) in the new comm_world
call mpi_cart_get(comm2d,2,dims,periods,coo,code)
! defining the indices of each subdomains
! the indices are the real indices in the global array 8x8
if(coo(1)==ndi-1) then
nni = nni_last
i2 = imax
i1 = i2-nni+1
else
i1 = rank/ndj*nni+1
i2 = i1+nni-1
endif
if(coo(2)==ndj-1) then
nnj = nnj_last
j2 = jmax
j1 = j2-nnj+1
else
j1 = MOD(rank,ndj)*nnj+1
j2 = j1+nnj-1
endif
! get the neighboors
adj = MPI_PROC_NULL
call MPI_CART_SHIFT(comm2d,1,1,adj(1),adj(2),code)
call MPI_CART_SHIFT(comm2d,0,1,adj(3),adj(4),code)
IF(adj(2)/=-2) THEN
coordsnw(1) = coo(1)-1
coordsnw(2) = coo(2)+1
IF(coordsnw(2)/=ndj.and.coordsnw(1)/=-1) THEN
call MPI_CART_RANK(comm2d,coordsnw,adj(5),code)
ENDIF
coordsne(1) = coo(1)+1
coordsne(2) = coo(2)+1
IF(coordsne(2)/=ndj.and.coordsne(1)/=ndi) THEN
call MPI_CART_RANK(comm2d,coordsne,adj(6),code)
ENDIF
ENDIF
IF(adj(1)/=-2) THEN
coordssw(1) = coo(1)-1
coordssw(2) = coo(2)-1
IF(coordssw(1)/=-1.and.coordssw(2)/=-1) THEN
call MPI_CART_RANK(comm2d,coordssw,adj(7),code)
ENDIF
coordsse(1) = coo(1)+1
coordsse(2) = coo(2)-1
IF(coordsse(1)/=ndi.and.coordsse(2)/=-1) THEN
call MPI_CART_RANK(comm2d,coordsse,adj(8),code)
ENDIF
ENDIF
! indices of the subdomain for each rank
! bigger than the actual since I need the interface
! for flux calculation (Finite volume)
iminR = MAX0(i1-1,1)
imaxR = MIN0(i2+1,imax)
jminR = MAX0(j1-1,1)
jmaxR = MIN0(j2+1,jmax)
allocate(local(iminR:imaxR,jminR:jmaxR))
local = rank
! FIN DEFINITION TABLEAU LOCAUX
! ENVOI DE MESSAGES
! new type
! call mpi_type_extent(mpi_double_precision,sizeofdouble,code)
!
! ! type coin /
! ! call mpi_type_vector(1,1,1,mpi_double_precision,type_base,code)
! size_glob = [imaxR-iminR+1, jmaxR-jminR+1]
! size_loc = [1, 1]
! if(rank==0) coo_init = [imaxR, jmaxR]
! coo_init = [iminR, jmaxR]
! call mpi_type_create_subarray(2,size_glob,size_loc,coo_init,mpi_order_fortran&
! ,mpi_double_precision,type_base,code)
! call mpi_type_commit(type_base,code)
! adj 1 -> S
! adj 2 -> N
! adj 3 -> W
! adj 4 -> E
! adj 5 -> NW
! adj 6 -> NE
! adj 7 -> SW
! adj 8 -> SE
! NW = North West
! SE = South East ...
! send NW / recv SE
call MPI_SENDRECV(local(iminR,jmaxR),1,MPI_INTEGER,adj(5),&
tag,local(imaxR,jminR),1,MPI_INTEGER,adj(8),&
tag,mpi_comm_world,stats,info)
! send NE / recv SW
call MPI_SENDRECV(local(imaxR,jmaxR),1,MPI_INTEGER,adj(6),&
tag,local(iminR,jminR),1,MPI_INTEGER,adj(7),&
tag,MPI_COMM_WORLD,stats,info)
! send SW / recv NE
call MPI_SENDRECV(local(iminR,jminR),1,MPI_INTEGER,adj(7),&
tag,local(imaxR,jmaxR),1,MPI_INTEGER,adj(6),&
tag,MPI_COMM_WORLD,stats,info)
! send SE / recv NW
call MPI_SENDRECV(local(imaxR,jminR),1,MPI_INTEGER,adj(8),&
tag,local(iminR,jmaxR),1,MPI_INTEGER,adj(5),&
tag,mpi_comm_world,stats,info)
call mpi_barrier(mpi_comm_world,code)
do i = iminR,imaxR
! print*, rank,"|",iminR,imaxR,jminR,jmaxR,"|",local(i,:)
print*, rank,"| ","|",local(i,:)
enddo
print*, ""
call mpi_finalize(code)
endprogram scatter
Edit : each sendrecv works by itself but using multiple sendrecv doesnt work.

Related

Calculate average of all distinct values within group

I have two columns with data.
One has labels for a group and a second displays values for items in each group. I would like to calculate for each group, the average of only those values that are distinct.
How can I do this in Stata?
EDIT:
See my dataset and desired result below:
Group_label Value
x 12
x 12
x 2
x 1
y 5
y 5
y 5
y 2
y 2
I want to generate the following average:
Group_label Value Average
x 12 5
x 12 5
x 2 5
x 1 5
y 5 3.5
y 5 3.5
y 5 3.5
y 2 3.5
y 2 3.5
So the average for x = (12 + 2 + 1) / 3 and for y = (5 + 2) / 2
I have tried the egen(mean) command but it selects all values for each group label.
I only want to select the distinct values.
This is a two-step solution. You first need to tag distinct values using tag() within egen. Then you use mean() within egen.
The most delicate point is that something like ... if tag will leave missing values in the result for observations not selected. How can you omit duplicated values from the calculation yet also spread the result to their observations? See Section 9 of this paper for the use of cond() together with mean() which is one way to do it, exemplified in the code, and perhaps the most transparent way too. See Section 10 of the same paper for another method, which amuses some people.
For a fairly detailed review of distinct observations, see https://www.stata-journal.com/sjpdf.html?articlenum=dm0042
clear
input str1 Group_label Value
x 12
x 12
x 2
x 1
y 5
y 5
y 5
y 2
y 2
end
egen tag = tag(Group_label Value)
egen mean = mean(cond(tag, Value, .)), by(Group_label)
list, sepby(Group_label)
+-------------------------------+
| Group_~l Value tag mean |
|-------------------------------|
1. | x 12 1 5 |
2. | x 12 0 5 |
3. | x 2 1 5 |
4. | x 1 1 5 |
|-------------------------------|
5. | y 5 1 3.5 |
6. | y 5 0 3.5 |
7. | y 5 0 3.5 |
8. | y 2 1 3.5 |
9. | y 2 0 3.5 |
+-------------------------------+
The following works for me:
clear
input str1 vlab val
"x" 12
"x" 12
"x" 2
"x" 1
"y" 5
"y" 5
"y" 5
"y" 2
"y" 2
end
bysort vlab: generate tag = val != val[_n-1]
bysort vlab: egen mean_val = mean(val) if tag == 1
list
+-----------------------------+
| vlab val tag mean_val |
|-----------------------------|
1. | x 12 1 5 |
2. | x 12 0 . |
3. | x 2 1 5 |
4. | x 1 1 5 |
5. | y 5 1 3.5 |
|-----------------------------|
6. | y 5 0 . |
7. | y 5 0 . |
8. | y 2 1 3.5 |
9. | y 2 0 . |
+-----------------------------+
EDIT:
If you also do:
bysort vlab: replace mean_val = mean_val[_n-1] if mean_val == .
You will get:
list
+-----------------------------+
| vlab val tag mean_val |
|-----------------------------|
1. | x 12 1 5 |
2. | x 12 0 5 |
3. | x 2 1 5 |
4. | x 1 1 5 |
5. | y 5 1 3.5 |
|-----------------------------|
6. | y 5 0 3.5 |
7. | y 5 0 3.5 |
8. | y 2 1 3.5 |
9. | y 2 0 3.5 |
+-----------------------------+

Fill with values from an earlier time point - Stata

I am trying to generate a variable that is filled using a sequence of values starting at time==1.
The sequence changes everytime the variable rest1w changes from 0 to 1 or vice versa.
Firstly, I think I need to generate x, that is where the sequence restarts (see below example dataset). In my example, this is uniform, but in my full dataset the change varies (i.e. it does not change at every 5th observation).
list time restload trainload rest1w x in 1/15
+-----------------------------------------+
| time restload trainload rest1w x |
|-----------------------------------------|
1. | 1 .1994715 .4780615 0 1 |
2. | 2 .2077734 .471063 0 2 |
3. | 3 .2157595 .4641159 0 3 |
4. | 4 .2234298 .4572202 0 4 |
5. | 5 .2307843 .4503757 0 5 |
|-----------------------------------------|
6. | 6 .2378229 .4435827 1 1 |
7. | 7 .2445457 .436841 1 2 |
8. | 8 .2509527 .4301506 1 3 |
9. | 9 .2570438 .4235116 1 4 |
10. | 10 .2628191 .4169239 1 5 |
|-----------------------------------------|
11. | 11 .2682785 .4103876 0 1 |
12. | 12 .2734221 .4039026 0 2 |
13. | 13 .2782499 .397469 0 3 |
14. | 14 .2827618 .3910867 0 4 |
15. | 15 .2869579 .3847558 0 5 |
+-----------------------------------------+
Secondly, I need to generate a variable load. Which as per below shows how I would like to restart from time==1 everytime the sequence restarts. That is, at the second sequence where rest1w==0, load!=trainload.
The rule is that for each new sequence of 0's the value for load again goes back to the start of time (where time==1). This is demonstrated by the load values in the second sequence of 0's being exactly the same as the first sequence. In other words, where time==1, trainload==.478 then load==.478; BUT where time==11, then load==.478 (the clock essentially restarts for load so time==1) and in sequence where time==15, load==.450 (the same load as for where time==5). This is why I wanted to generate x, as I think I could just use that as my new time variable.
+-----------------------------------------+
| time restload trainload rest1w x load
|-----------------------------------------
1. | 1 .1994715 .4780615 0 1 .4780615
2. | 2 .2077734 .471063 0 2 .471063
3. | 3 .2157595 .4641159 0 3 .4641159
4. | 4 .2234298 .4572202 0 4 .4572202
5. | 5 .2307843 .4503757 0 5 .4503757
|-----------------------------------------
6. | 6 .2378229 .4435827 1 1 .1994715
7. | 7 .2445457 .436841 1 2 .2077734
8. | 8 .2509527 .4301506 1 3 .2157595
9. | 9 .2570438 .4235116 1 4 .2234298
10. | 10 .2628191 .4169239 1 5 .2307843
|-----------------------------------------
11. | 11 .2682785 .4103876 0 1 .4780615
12. | 12 .2734221 .4039026 0 2 .471063
13. | 13 .2782499 .397469 0 3 .4641159
14. | 14 .2827618 .3910867 0 4 .4572202
15. | 15 .2869579 .3847558 0 5 .4503757
+-----------------------------------------+
The below code only gives me an entry for where _n==1:
gen load==.
replace load = restload[_n==1] if rest1w==1
And I like the use of levelsof but haven't been able to get it to work (although it might work once I have generated x, but when using time it doesn't restart the sequence obviously).
gen load=.
levelsof x, local(levels)
foreach l of local levels {
replace load=trainload if rest1w==0
replace load=restload if rest1w==1
}
Thanks for any help!
I ended up cross-posting this on statalist.org and got two workable answers.
http://www.statalist.org/forums/forum/general-stata-discussion/general/1355917-fill-with-values-from-an-earlier-time-point
These were:
gen newtime = 1 if rest1w[_n - 1] != rest1w
replace newtime = newtime[_n - 1] + 1 if newtime == .
gen newload = cond(rest1w == 0, trainload[newtime], restload[newtime])
and...
gen newtime = 1
replace newtime = newtime[_n-1] + 1 if rest1w == rest1w[_n-1]
gen newload = .
replace newload = restload[newtime] if rest1w == 1
replace newload = trainload[newtime] if rest1w == 0

using get() and appending those chars to a string is giving me nonsense

I am supposed to use a program that (one char at a time) reads a GPS log file, and parses it. I'm getting normal responses when I use some .log files, and not with others (especially not long ones).
The code that grabs the information and stores it is below:
class ReadInput{
public:
string massiveString;
};
void ReadInput::assign(string inMassiveString){ //assign the string passed to it to the object
this->massiveString.empty(); //clear whatever might be in the string
this->massiveString=inMassiveString; //copy the string into where it belongs.
}
int main(int argc, const char * argv[]) {
ifstream inputStream;
inputStream.open("gps3.log");
int i = 1;
char temp;
string tempString;
ReadInput *sentence[258];
//this is the part that grabs the characters
while(1){ //
if (!inputStream.eof()){ //
inputStream.get(temp);
if (temp!=' ') {
tempString.append(&temp); //this appends the characters to a temporary string
// cout << i << "\t" << tempString << endl; //testing the input
}
if (temp == '\n') { //if this is the end of the line, pass it on.
sentence[i]=new ReadInput;
sentence[i]->assign(tempString); //the part that copies the temporary string to the object
cout << i << "\t" << sentence[i]->massiveString << endl; //another testing line
i++;
tempString.clear();
}
}
else{
break;
}
}
}
it is very important that I read it character by character.
what it should be reading looks like this:
$GPGGA,155008.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*53
and here is an example of a problematic reading. in the format of
entry#, stringStoredInEntry.
1 $GPGGA,155002.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*58
2 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
3 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48
4 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59
5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B
7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71
8 F
9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79
10 $
G
P
G
G
A
,
1
5
5
0
0
4
.
0
0
0
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
1
,
0
5
,
1
.
7
,
8
9
.
1
,
M
,
-
3
3
.
6
,
M
,
,
0
0
0
0
*
5
E
11 $
G
P
G
S
A
,
A
,
3
,
1
7
,
0
6
,
2
8
,
0
2
,
2
4
,
,
,
,
,
,
,
,
3
.
2
,
1
.
7
,
2
.
7
*
3
E
12 $
G
P
R
M
C
,
1
5
5
0
0
4
.
0
0
0
,
A
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52
29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43
31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A
2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E
!33 $!G!P!R!M!C!,!1!5!5!0!1!0!.!0!0!0!,!A!,!3!7!3!2!.!7!2!3!9!,!N!,!0!7!7!2!6!.!9!9!5!6!,!W!,!0!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*V*,*3*,*1*,*1*2*,*1*7*,*6*4*,*0*3*8*,*4*1*,*0*6*,*6*3*,*2*5*0*,*4*2*,*2*8*,*4*4*,*1*5*0*,*4*3*,*0*2*,*2*5*,*2*3*9*,*3*8***7*F*
+43 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+
,44 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F,
-45 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8-
.46 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E.
/47 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/
048 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0
149 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1
250 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2
351 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3
452 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444
553 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5
654 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6
755 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757
856 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8
957 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9
:58 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A:
;59 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E;
<60 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C<
=61 $=G=P=G=S=V=,=3=,=2=,=1=2=,=2=4=,=1=7=,=2=9=4=,=1=3=,=1=0=,=2=1=,=1=7=6=,=2=2=,=1=2=,=2=5=,=3=1=6=,=2=6=,=5=1=,=3=6=,=2=2=3=,=3=6=*=7=7=
>62 $>G>P>G>S>V>,>3>,>3>,>1>2>,>2>0>,>3>4>,>0>6>1>,>,>0>1>,>0>7>,>0>5>1>,>,>0>4>,>0>5>,>0>5>2>,>,>3>2>,>0>2>,>0>3>2>,>*>7>E>
?63 $?G?P?R?M?C?,?1?5?5?0?1?8?.?0?0?0?,?A?,?3?7?3?2?.?7?2?3?9?,?N?,?0?7?7?2?6?.?9?9?5?6?,?W?,?0?.?0?0?,?8?0?.?3?0?,?1?4?1?0?1?4?,?,?,?A?*?4?3?
#64 $#G#P#G#G#A#,#1#5#5#0#1#9#.#0#0#0#,#3#7#3#2#.#7#2#3#9#,#N#,#0#7#7#2#6#.#9#9#5#6#,#W#,#1#,#0#4#,#2#.#3#,#8#8#.#9#,#M#,#-#3#3#.#6#,#M#,#,#0#0#0#0#*#5#D#
A65 $AGAPAGASAAA,AAA,A3A,A1A7A,A0A6A,A2A8A,A0A2A,A,A,A,A,A,A,A,A,A4A.A8A,A2A.A3A,A4A.A3A*A3A0A
B66 $BGBPBRBMBCB,B1B5B5B0B1B9B.B0B0B0B,BAB,B3B7B3B2B.B7B2B3B9B,BNB,B0B7B7B2B6B.B9B9B5B6B,BWB,B0B.B0B0B,B8B0B.B3B0B,B1B4B1B0B1B4B,B,B,BAB*B4B2B
B67 $GPGGA,155007.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5C
C1 $GPGGA,155002.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*58
21 $GPRMC,155007.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*4D
22 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E,1.7,89.0,M,-33.6,M,,0000*53
23 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
34 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48
25 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,24,51,36,223,35*74
46 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59
27 $
5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B
7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71
8 F
9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79
10 $
G
P
G
G
A
,
1
5
5
0
0
4
.
0
0
0
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
1
,
0
5
,
1
.
7
,
8
9
.
1
,
M
,
-
3
3
.
6
,
M
,
,
0
0
0
0
*
5
E
11 $
G
P
G
S
A
,
A
,
3
,
1
7
,
0
6
,
2
8
,
0
2
,
2
4
,
,
,
,
,
,
,
,
3
.
2
,
1
.
7
,
2
.
7
*
3
E
12 $
G
P
R
M
C
,
1
5
5
0
0
4
.
0
0
0
,
A
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
0
.
0
0
,
8
0
.
3
0
8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52
1
29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 4
1
30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43
1
31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A
,
2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E
,
!3 $!G!P!R!M!C!,!1!5!5!0!1!0!.!0!0!0!,!A!,!3!7!3!2!.!7!2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)8),)0)2),)2)4),),),),),),),),)3).)2),)1).)7),)2).)7)*)3)E)
)
*2 $*G*P*G*S*V*,*3*,*1*,*1*2*,*1*7*,*6*4*,*0*3*8*,*4*1*,*0*6*,*6*3*,*2*5*0*,*4*2*,*2*8*,*4*4*,*1*5*0*,*4*3*,*0*2*,*2*5*,*2*3*9*,*3*8***7*F*
*
+3 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+
+
,4 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F,
,
-5 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8-
-
.6 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E.
.
/7 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/
/
08 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0
0
19 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1
1
20 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2
2
31 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3
3
42 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444
4
53 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5
5
64 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6
6
75 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757
7
86 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8
8
97 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9
9
:8 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A:
:
;9 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E;
;
<0 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C<
<
=1 $=G=P=G=S=V=,=3=,=2=,=1=2=,=2=4=,=1=7=,=2=9=4=,=1=3=,=1=0=,=2=1=,=1=7=6=,=2=2=,=1=2=,=2=5=,=3=1=6=,=2=6=,=5=1=,=3=6=,=2=2=3=,=3=6=*=7=7=
=
>2 $>G>P>G>S>V>,>3>,>3>,>1>2>,>2>0>,>3>4>,>0>6>1>,>,>0>1>,>0>7>,>0>5>1>,>,>0>4>,>0>5>,>0>5>2>,>,>3>2>,>0>2>,>0>3>2>,>*>7>E>
>
?3 $?G?P?R?M?C?,?1?5?5?0?1?8?.?0?0?0?,?A?,?3?7?3?2?.?7?2?3?9?,?N?,?0?7?7?2?6?.?9?9?5?6?,?W?,?0?.?0?0?,?8?0?.?3?0?,?1?4?1?0?1?4?,?,?,?A?*?4?3?
?
#4 $#G#P#G#G#A#,#1#5#5#0#1#9#.#0#0#0#,#3#7#3#2#.#7#2#3#9#,#N#,#0#7#7#2#6#.#9#9#5#6#,#W#,#1#,#0#4#,#2#.#3#,#8#8#.#9#,#M#,#-#3#3#.#6#,#M#,#,#0#0#0#0#*#5#D#
#
A5 $AGAPAGASAAA,AAA,A3A,A1A7A,A0A6A,A2A8A,A0A2A,A,A,A,A,A,A,A,A,A4A.A8A,A2A.A3A,A4A.A3A*A3A0A
A
B6 $BGBPBRBMBCB,B1B5B5B0B1B9B.B0B0B0B,BAB,B3B7B3B2B.B7B2B3B9B,BNB,B0B7B7B2B6B.B9B9B5B6B,BWB,B0B.B0B0B,B8B0B.B3B0B,B1B4B1B0B1B4B,B,B,BAB*B4B2B
B
1-7 are perfect. great.
then 8 gets weird.
and it never get's normal.
Where is this coming from? is this problems with the .log file? is this my code? where is this nonsense creeping in? and, more importantly, how is it messing with the integers to the left? How can anything consistently make an integer that nonsensical?
using tempString.push_back(temp);
instead of tempString.append(&temp);
fixed everything.

List of lists in Scala? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a grid (that is declared as a List (of Lists)) and each 3*3 block for example is indexed as 0, the next 3*3 block is indexed as 1, etc.
I need to write a method that returns the elements (integers) in a particular block given an index.
For example : b(0) should give 1 0 0 4 1 2 7 0 0
1 0 0 | 4 0 2 | 5 0 7 |
4 1 2 | 0 0 3 | 6 8 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
9 0 2 | 0 4 3 | 1 8 0 |
7 1 0 | 0 8 9 | 8 0 0 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
4 0 2 | 0 1 9 | 6 2 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------
implicit class MatrixOps[T](val l: List[List[T]]) extends AnyVal {
def getBlock(index: Int, size: Int = 3): List[T] = {
l map (_ drop index * size) take size flatMap (_ take size)
}
}
override def main(args: Array[String]): Unit = {
val x = List(
List(1, 2, 3),
List(4, 5, 6)
)
println(x.getBlock(0))
println(x.getBlock(1))
}
Try with following function,
def block(i:Int, grid:List[List[Int]]) = grid.
grouped(3).
toList(i / 3).
map(_.grouped(3).toList(i % 3))
Here's that example with nice print method
def print(q:List[List[Int]]) = q.map(_.mkString("\t")).mkString("\n")
val grid = (1 to 81).toList.grouped(9).toList
scala> print(block(1))
res37: String =
4 5 6
13 14 15
22 23 24
(Sorry, Im too lazy to write a sudoku board as an example :) )
To get the list of integers, try flattern.
scala> block(1).flattern
res38: List[Int] = List(4, 5, 6, 13, 14, 15, 22, 23, 34)

Stata: Maximum number of consecutive occurrences of the same value across variables

Observations in my dataset are players, and binary variables temp1 up are equal to 1 if the player made a move, and equal to zero otherwise.
I would like to to calculate the maximum number of consecutive moves per player.
+------------+------------+-------+-------+-------+-------+-------+-------+
| simulation | playerlist | temp1 | temp2 | temp3 | temp4 | temp5 | temp6 |
+------------+------------+-------+-------+-------+-------+-------+-------+
| 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 2 | 1 | 0 | 0 | 0 | 1 | 1 |
+------------+------------+-------+-------+-------+-------+-------+-------+
My idea was to generate auxiliary variables in a loop, which would count consecutive duplicates and then apply egen, rowmax():
+------------+------------+------+------+------+------+------+------+------+
| simulation | playerlist | aux1 | aux2 | aux3 | aux4 | aux5 | aux6 | _max |
+------------+------------+------+------+------+------+------+------+------+
| 1 | 1 | 0 | 1 | 2 | 3 | 0 | 0 | 3 |
| 1 | 2 | 1 | 0 | 0 | 0 | 1 | 2 | 2 |
+------------+------------+------+------+------+------+------+------+------+
I am struggling with introducing a local counter variable that would be incrementally increased by 1 if consecutive move is made, and would be reset to zero otherwise (the code below keeps auxiliary variables fixed..):
quietly forval i = 1/42 { /*42 is max number of variables temp*/
local j = 1
gen aux`i'=.
local j = `j'+1
replace aux`i'= `j' if temp`i'!=0
}
Tactical answer
You can concatenate your move* variables into a single string and look for the longest substring of 1s.
egen history = concat(move*)
gen max = 0
quietly forval j = 1/6 {
replace max = `j' if strpos(history, substr("111111", 1, `j'))
}
If the number is much more than 6, use something like
 local lookfor : di _dup(42) "1" 
quietly forval j = 1/42 {
replace max = `j' if strpos(history, substr("`lookfor'", 1, `j'))
}
Compare also http://www.stata-journal.com/article.html?article=dm0056
Strategic answer
Storing a sequence rowwise is working against the grain so far as Stata is concerned. Much more flexibility is available if you reshape long and tsset your data as panel data. Note that the code here uses tsspell which must be installed from SSC using ssc inst tsspell.
tsspell is dedicated to identifying spells or runs in which some condition remains true. Here the condition is that a variable is 1 and since the only other allowed value is 0 that is equivalent to a variable being positive. tsspell creates three variables, giving spell identifier, sequence within spell and whether the spell is ending. Here the maximum length of spell is just the maximum sequence number for each game.
. input simulation playerlist temp1 temp2 temp3 temp4 temp5 temp6
simulat~n playerl~t temp1 temp2 temp3 temp4 temp5 temp6
1. 1 1 0 1 1 1 0 0
2. 1 2 1 0 0 0 1 1
3. end
. reshape long temp , i(sim playerlist) j(seq)
(note: j = 1 2 3 4 5 6)
Data wide -> long
-----------------------------------------------------------------------------
Number of obs. 2 -> 12
Number of variables 8 -> 4
j variable (6 values) -> seq
xij variables:
temp1 temp2 ... temp6 -> temp
-----------------------------------------------------------------------------
. egen id = group(sim playerlist)
. tsset id seq
panel variable: id (strongly balanced)
time variable: seq, 1 to 6
delta: 1 unit
. tsspell, p(temp)
. egen max = max(_seq), by(id)
. l
+--------------------------------------------------------------------+
| simula~n player~t seq temp id _seq _spell _end max |
|--------------------------------------------------------------------|
1. | 1 1 1 0 1 0 0 0 3 |
2. | 1 1 2 1 1 1 1 0 3 |
3. | 1 1 3 1 1 2 1 0 3 |
4. | 1 1 4 1 1 3 1 1 3 |
5. | 1 1 5 0 1 0 0 0 3 |
|--------------------------------------------------------------------|
6. | 1 1 6 0 1 0 0 0 3 |
7. | 1 2 1 1 2 1 1 1 2 |
8. | 1 2 2 0 2 0 0 0 2 |
9. | 1 2 3 0 2 0 0 0 2 |
10. | 1 2 4 0 2 0 0 0 2 |
|--------------------------------------------------------------------|
11. | 1 2 5 1 2 1 2 0 2 |
12. | 1 2 6 1 2 2 2 1 2 |
+--------------------------------------------------------------------+