Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiprocess write fails with lost data - ignoring ProcessSynchronizer? #701

Open
choosehappy opened this issue Feb 15, 2021 · 4 comments
Open

Comments

@choosehappy
Copy link

I think i am following the documentation correctly, but this does not appear to work as expected. After a few hours of debugging I'm stumped, but it does not look like "This array is safe to read or write from multiple processes." as the documentation suggests (https://zarr.readthedocs.io/en/stable/tutorial.html Parallel computing and synchronization section)

It looks like each process is not aware of how long the current array is, writes wherever it thinks is the last piece, potentially overwriting existing data. Overall it appears like the existence of synchronizer is completely ignored?

Any thoughts or comments would be greatly appreciated

import numpy as np
import zarr
from multiprocessing import Pool
import functools
from zarr import blosc
blosc.use_threads=False



def maptobins(i,step_size,hdf5_file):
    vals=np.arange(i,i+step_size)
    hdf5_file.append(vals)
    return (i,vals)


if __name__ == '__main__':

    step_size=100
    items = 1000
    
    print("pool start")
    p=Pool(32)
    print("pool job exec")
    
    synchronizer = zarr.ProcessSynchronizer(f'data/example_XX.sync')
    hdf5_file=zarr.open_array(f'data/testData5h_1B.zarr', mode='a',  shape=0, 
                           synchronizer=synchronizer, dtype='i4')



    results=p.map(functools.partial(maptobins, step_size=step_size, hdf5_file=hdf5_file), range(0, items, step_size))


    #print(results)
    val= zarr.open(f'data/testData5h_1B.zarr', mode='r')
    print(len(val))
    print(val[:])

Problem description

This is a bare example, I create a ProcessSynchronizerand open an array stored locally (Linux) with that processor on a server and launch 32 processes.

Each of 10 process tries to add 100 integers to the array.

The expected result is 1000 integers, between 1 and 1000

What I receive is highly variably, and most of the time much less than 1000 integeters, for example in this case, 500 integers resulted:

500
[  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 600 601 602 603 604 605 606 607
 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
 788 789 790 791 792 793 794 795 796 797 798 799 900 901 902 903 904 905
 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
 996 997 998 999 800 801 802 803 804 805 806 807 808 809 810 811 812 813
 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
 886 887 888 889 890 891 892 893 894 895 896 897 898 899]

deleting the data directory and running again results in only 400 values:

400
[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
 190 191 192 193 194 195 196 197 198 199 800 801 802 803 804 805 806 807
 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
 988 989 990 991 992 993 994 995 996 997 998 999 700 701 702 703 704 705
 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
 796 797 798 799]

Version and installation information

Please provide the following:

  • Value of zarr.__version__
    '2.6.1'
  • Value of numcodecs.__version__
    '0.7.3'
  • Version of Python interpreter

Python 3.8.5

  • Operating system (Linux/Windows/Mac)

Linux

  • How Zarr was installed (e.g., "using pip into virtual environment", or "using conda")

pip3 install into base o/s

@choosehappy choosehappy changed the title Multiprocess write fails with lost data Multiprocess write fails with lost data - ignoring ProcessSynchronizer? Feb 15, 2021
@joshmoore
Copy link
Member

@choosehappy : can you share how you are launching this across your cluster?

@choosehappy
Copy link
Author

On the command line:

axj232@easley:/data/axj232$ python3 zarr_minimum_error.py
pool start
pool job exec
363000
axj232@easley:/data/axj232$ rm -rf data*
axj232@easley:/data/axj232$ python3 zarr_minimum_error.py
pool start
pool job exec
113000
axj232@easley:/data/axj232$ rm -rf data*
axj232@easley:/data/axj232$ python3 zarr_minimum_error.py
pool start
pool job exec
166000
axj232@easley:/data/axj232$

I've also confirmed the behavior in windows which has python 3.7.5 (deleting "data" directory in between runs via windows explorer).

note the first time it works as expected (suggesting no code error to me), but the other 2 times it does not, leaving me to believe it is some type of race condition

C:\Users\Andrew\Desktop\deleteme\t>python "zarr_minimum_error.py"
pool start
pool job exec
10000

C:\Users\Andrew\Desktop\deleteme\t>python "zarr_minimum_error.py"
pool start
pool job exec
4900

C:\Users\Andrew\Desktop\deleteme\t>python "zarr_minimum_error.py"
pool start
pool job exec
1400

happy to provide any additional details!

@choosehappy
Copy link
Author

btw, to avoid confusion, my laptop is less powerful than the Linux machine, so I changed the # of items

for the Linux machine:

    step_size=1000
    items = 1000000

for the windows laptop:

    step_size=100
    items = 10000

@choosehappy
Copy link
Author

For those interested, I've hacked around this bug by using an array of multiprocessing locks, and requiring obtaining the lock before writing to the zarr file. in this manner, ProcessSynchronizer aren't needed

would be interested in hearing if anyone sees any issues with this besides a bit of a performance hit?

import numpy as np
import zarr
from tqdm.autonotebook import tqdm
from multiprocessing import Pool, Lock
import functools

def maptobins(i,bins,step_size):

    rand=np.random.rand(step_size)    
    b=np.searchsorted(bins,rand)-1
    for bi in np.random.permutation(np.arange(100)): # randomize the order of the files so that each process tries to write to a different file and thus reduces chances of locking
        with locks[bi]:
            zarr_file=zarr.open(f'data/testData5h_1B_{bi}.zarr', mode='a',  shape=0, chunks=(65535, ), dtype='f2')
            zarr_file.append(rand[b==bi])
            
    return 1


def init(l):
    global locks
    locks = l


if __name__ == '__main__':
    nitems = int(1e9)
    step_size=int(1e7)

    xmax=1.0
    xmin=0.0
    bins=np.linspace(xmin,xmax,num=100)

    locks=[ Lock() for x in range(100) ]
    
    print("pool start")
    p = Pool(processes=32,initializer=init, initargs=(locks,))
    print("pool job exec")
    results=p.map(functools.partial(maptobins, bins=bins, step_size=step_size),range(0, nitems, step_size))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants