0

I'm trying in this code to search a given element into an array that has random numbers. With scatter and reduce i want just to count how many times the element exists in the array. I scatter the array and the searching is being done in the splitting sub-arrays. Every splitting array finds how many times the element is found in this sub-array and then with reduce i gather all the times that the element is found in the whole primary array.


boo
  • 11
  • 3
  • I think the line `a=comm.bcast(a,root=0) ` shouldn't be there (?) – Demi-Lune Dec 27 '19 at 13:14
  • I think recvbuf is 2-dimensional array size of [1,N/nprocs] and you trying to compare array of size N/nprocs with single value in line recvbuf[i]==b. May be you need 1-dimesnional array of size N/nprocs here – CrazyElf Dec 27 '19 at 13:24
  • This line is for the broadcast of the array to the other ranks. The primary array obtained random elements only on rank 0 and this is the reason you have to broadcast the array in order the other ranks be informed of the elements of the array. – boo Dec 27 '19 at 13:46
  • If i make the recvbuf 1-d :recvbuf= np.empty ([N/nprocs], dtype=np.int) ........................i have these faults: File "search.py", line 29, in if(recvbuf[i]==b): IndexError: index 50 is out of bounds for axis 0 with size 50 Traceback (most recent call last): File "search.py", line 29, in if(recvbuf[i]==b): IndexError: index 50 is out of bounds for axis 0 with size 50 – boo Dec 27 '19 at 13:50
  • I meant I don't see the point in using *both* bcast and Scatter. Either use one or the other, no? – Demi-Lune Dec 27 '19 at 14:47

1 Answers1

1

The error is triggered because recv_buf.shape is (1,N//nprocs).

Make it 1-D recvbuf= np.empty ([N//nprocs], 'd'), and it will be ok (note my use of //, so your program works for both py2 and py3).

By the way, you may want to look at this to count occurences: How to count the occurrence of certain item in an ndarray in Python?

Also, you're mixing different types: a on process zero is not typed (so it is int64), a on the other processes is typed 'i' (so it is int32, but you don't use it so it's a real issue), and recvbuf is 'f' (float32).

Demi-Lune
  • 1,868
  • 2
  • 15
  • 26
  • If i make the recvbuf 1-d :recvbuf= np.empty ([N/nprocs], dtype=np.int) ........................i have these faults: File "search.py", line 29, in if(recvbuf[i]==b): IndexError: index 50 is out of bounds for axis 0 with size 50 Traceback (most recent call last): File "search.py", line 29, in if(recvbuf[i]==b): IndexError: index 50 is out of bounds for axis 0 with size 50 – boo Dec 27 '19 at 14:02
  • That's simply your `(N/nprocs)+1` that shouldn't have the +1 (you should double check the integer division rounding with a few examples to be 100% sure) – Demi-Lune Dec 27 '19 at 14:46
  • You typed `recvbuf` as 'd' (float), so if you print it, you'll see it doesn't contain what you expect. – Demi-Lune Dec 27 '19 at 15:00
  • First: ', array([ 7, 67, 11, 50, 69, 15, 16, 73, 35, 92, 21, 6, 37, 22, 28, 38, 72, 56, 51, 73, 20, 9, 28, 53, 96, 8, 94, 66, 62, 73, 52, 77, 37, 0, 43, 25, 34, 86, 47, 0, 65, 83, 67, 29, 47, 41, 39, 62, 61, 44, 75, 24, 66, 76, 42, 64, 16, 42, 2, 23, 10, 18, 0, 94, 43, 38, 47, 22, 95, 6, 74, 48, 2, 35, 33, 93, 66, 76, 63, 86, 80, 30, 93, 12, 85, 25, 2, 90, 91, 30, 40, 16, 46, 42, 74, 19, 71, 60, 80, 67])) ('The element 20 found:', 0) although b=20 exists on the array...why this happens?? – boo Dec 27 '19 at 15:01
  • You're really mixing many different types: `a`on process zero is not typed (so it is int64), `a`on the other processes is typed 'i' (so it is int32), and `recvbuf` is 'f' (float32). – Demi-Lune Dec 27 '19 at 15:05