-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_reffree_gpu_align.py
1214 lines (1013 loc) · 56.3 KB
/
test_reffree_gpu_align.py
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
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
1000
#!/usr/local/EMAN2/bin/python
from __future__ import print_function
#******************************************************************************
#*
#* GPU based reference free alignment
#*
#* Author (C) Szu-Chi Chung 2020 ([email protected])
#* Cheng-Yu Hung 2020 ([email protected])
#* Huei-Lun Siao 2020 ([email protected])
#* Hung-Yi Wu 2020 ([email protected])
#* Markus Stabrin 2019 ([email protected])
#* Fabian Schoenfeld 2019 ([email protected])
#* Thorsten Wagner 2019 ([email protected])
#* Tapu Shaikh 2019 ([email protected])
#* Adnan Ali 2019 ([email protected])
#* Luca Lusnig 2019 ([email protected])
#* Toshio Moriya 2019 ([email protected])
#* Pawel A.Penczek, 09/09/2006 ([email protected])
#*
#* Copyright (C) 2020 SABID Laboratory, Institute of Statistical Science, Academia Sinica
#* Copyright (c) 2019 Max Planck Institute of Molecular Physiology
#* Copyright (c) 2000-2006 The University of Texas - Houston Medical School
#*
#* This program is free software: you can redistribute it and/or modify it
#* under the terms of the GNU General Public License as published by the Free
#* Software Foundation, either version 3 of the License, or (at your option) any
#* later version.
#*
#* This program is distributed in the hope that it will be useful, but WITHOUT
#* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License along with
#* this program. If not, please visit: http://www.gnu.org/licenses/
#*
#******************************************************************************/
import os
import global_def
from global_def import *
from user_functions import *
from optparse import OptionParser
import sys
import sparx as spx
###############################################################
def kernel_sum_oe(gpu_ptr, nima, nx, gpu_refs, numref):
class CustomGpuArray(object):
def __init__(self, shape, dtype):
if (shape.__class__ is not (1,).__class__):
shape = (shape,)
self.shape = shape
self.ptr = gpu_ptr# <-- a RAW GPU pointer
# def __del__(self):
# cp.cuda.runtime.free(self.ptr) # <-- malloc/free should be a pair
@property
def __cuda_array_interface__(self):
desc = {
'shape': self.shape,
'version': 0,
'data': (self.ptr, False),
'typestr': '<f4', # <-- because of float32, set '<f4',
'descr': [('', '<f4')]
}
return desc
obj = CustomGpuArray((nima,nx,nx), cp.float32)
gpu_batch_img = cp.asarray(obj, cp.float32)
gpu_ref_ave = cp.zeros((numref, 2, nx, nx), cp.float32)
for ref_id in range(numref):
even_indices = (cp.where(gpu_refs[::2]==ref_id)[0])*2
gpu_ref_ave[ref_id][0] = cp.sum(gpu_batch_img[even_indices], axis=0)
odd_indices = (cp.where(gpu_refs[1::2]==ref_id)[0])*2+1
gpu_ref_ave[ref_id][1] = cp.sum(gpu_batch_img[odd_indices], axis=0)
obj = None
return gpu_ref_ave
########################################################### GPU
import cupy as cp
########################################################### GPU
import math
import numpy as np
import ctypes
from EMAN2 import EMNumPy
CUDA_PATH = os.path.join(os.path.dirname(__file__), "cuda", "")
cu_module = ctypes.CDLL( CUDA_PATH + "gpu_aln_pack.so" )
float_ptr = ctypes.POINTER(ctypes.c_float)
cu_module.ref_free_alignment_2D_init.restype = ctypes.c_ulonglong
cu_module.pre_align_init.restype = ctypes.c_ulonglong
cu_module.pre_align_run_m.restype = ctypes.c_ulonglong
class Freezeable( object ):
def freeze( self ):
self._frozen = None
def __setattr__( self, name, value ):
if hasattr( self, '_frozen' )and not hasattr( self, name ):
raise AttributeError( "Error! Trying to add new attribute '%s' to frozen class '%s'!" % (name,self.__class__.__name__) )
object.__setattr__( self, name, value )
class AlignConfig( ctypes.Structure, Freezeable ):
_fields_ = [ # data param
("sbj_num", ctypes.c_uint), # number of subject images we want to align
("ref_num", ctypes.c_uint), # number of reference images we want to align the subjects to
("img_dim", ctypes.c_uint), # image dimension (in both x- and y-direction)
# polar sampling parameters
("ring_num", ctypes.c_uint), # number of rings when converting images to polar coordinates
("ring_len", ctypes.c_uint), # number of rings when converting images to polar coordinates
# shift parameters
("shift_step", ctypes.c_float), # step range when applying translational shifts
("shift_rng_x", ctypes.c_float), # translational shift range in x-direction
("shift_rng_y", ctypes.c_float) ] # translational shift range in y-direction
class AlignParam( ctypes.Structure, Freezeable ):
_fields_ = [ ("sbj_id", ctypes.c_int),
("ref_id", ctypes.c_int),
("shift_x", ctypes.c_float),
("shift_y", ctypes.c_float),
("angle", ctypes.c_float),
("mirror", ctypes.c_bool) ]
def __str__(self):
return "s_%d/r_%d::(%d,%d;%.2f)" % (self.sbj_id, self.ref_id, self.shift_x, self.shift_y, self.angle) \
+("[M]" if self.mirror else "")
aln_param_ptr = ctypes.POINTER(AlignParam)
def get_c_ptr_array( emdata_list ):
ptr_list = []
for img in emdata_list:
img_np = EMNumPy.em2numpy( img )
assert img_np.flags['C_CONTIGUOUS'] == True
assert img_np.dtype == np.float32
img_ptr = img_np.ctypes.data_as(float_ptr)
ptr_list.append(img_ptr)
return (float_ptr*len(emdata_list))(*ptr_list)
def print_gpu_info( cuda_device_id ):
cu_module.print_gpu_info( ctypes.c_int(cuda_device_id) )
########################################################### GPU
###############################################################
def ali2d_base_gpu_isac_CLEAN(
stack, outdir, maskfile=None, ir=1, ou=-1, rs=1, xr="4 2 1 1", yr="-1", ts="2 1 0.5 0.25",
nomirror = False, dst=0.0, center=-1, maxit=0, CTF=False, snr=1.0,
Fourvar=False, user_func_name="ref_ali2d", random_method = "", log = None,
number_of_proc = 1, myid = 0, main_node = 0, mpi_comm = None, write_headers = False,
mpi_gpu_proc=False, gpu_class_limit=0, cuda_device_occ=0.9):
"""
ISAC hardcoded defaults:
maskfile = None
ir = 1 # inner radius
rs = 1 # ring step
dst = 90.0
maxit = 14
snr = 1.0
Fourvar = False
user_func_name = "ref_ali2d"
random_method = ""
write_headers = False
PROFILE-GPU Toxin
alignment 52s
(..) 0s
resampling 56s <-- resampling an issue but already spread across all mpi_procs
sxheader 2s
PROFILE-CPU
alignment 169s
(..) 0
resampling 57 <--
sxheader 2
"""
#----------------------------------[ local imports ]
import os
import sys
import mpi
import math
import time
import alignment
import statistics
import fundamentals
import utilities as util
from filter import filt_ctf
from pixel_error import pixel_error_2D
import user_functions
from sp_applications import MPI_start_end
user_func = user_functions.factory[user_func_name]
# sanity check: gpu procs sound off
if not mpi_gpu_proc: return []
#----------------------------------[ setup ]
print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: MPI proc["+str(myid)+"] run pre-alignment CPU setup" )
# mpi communicator sanity check
assert mpi_comm != None
# set alignment search ranges
yrng = xrng = util.get_input_from_string(xr)
step = util.get_input_from_string(ts)
# polar conversion (ring) parameters
first_ring = int(ir)
last_ring = int(ou)
rstep = int(rs)
# alignment iterations
if int(maxit)==0:
max_iter = 10
auto_stop = True
else:
max_iter = int(maxit)
auto_stop = False
# determine global index values of particles handled by this process
data = stack
total_nima = len(data)
total_nima = mpi.mpi_reduce(total_nima, 1, mpi.MPI_INT, mpi.MPI_SUM, 0, mpi_comm)
total_nima = mpi.mpi_bcast (total_nima, 1, mpi.MPI_INT, main_node, mpi_comm)[0]
list_of_particles = list(range(total_nima))
image_start, image_end = MPI_start_end(total_nima, number_of_proc, myid)
list_of_particles = list_of_particles[image_start:image_end] # list of global indices
nima = len(list_of_particles)
assert( nima == len(data) ) # sanity check
# read nx and broadcast to all nodes
# NOTE: nx is image size and images are assumed to be square
if myid == main_node:
nx = data[0].get_xsize()
else:
nx = 0
nx = util.bcast_number_to_all(nx, source_node=main_node, mpi_comm=mpi_comm)
if CTF:
phase_flip = True
else:
phase_flip = False
CTF = False # okay..?
# set default value for the last ring if none given
if last_ring == -1: last_ring = nx/2-2
# sanity check for last_ring value
if last_ring + max([max(xrng), max(yrng)]) > (nx-1) // 2:
ERROR( "Shift or radius is too large - particle crosses image boundary", "ali2d_MPI", 1 )
# mask (note: ISAC hardcodes default circular mask)
mask = util.model_circle(last_ring, nx, nx)
# image center
cny = cnx = nx/2+1
mode = "F"
ctf_2_sum = None
# create/reset alignment parameters
for im in range(nima):
data[im].set_attr( 'ID', list_of_particles[im] )
util.set_params2D( data[im], [0.0, 0.0, 0.0, 0, 1.0], 'xform.align2d' )
st = Util.infomask( data[im], mask, False )
data[im] -= st[0]
if phase_flip:
data[im] = filt_ctf(data[im], data[im].get_attr("ctf"), binary = True)
# precalculate rings & weights
numr = alignment.Numrinit( first_ring, last_ring, rstep, mode )
wr = alignment.ringwe( numr, mode )
# initialize data for the reference preparation function [the what now?]
if myid == main_node:
ref_data = [mask, center, None, None]
sx_sum = 0.0
sy_sum = 0.0
a0 = -1.0e22
recvcount = []
disp = []
for i in range(number_of_proc):
ib, ie = MPI_start_end(total_nima, number_of_proc, i)
recvcount.append(ie-ib)
if i == 0:
disp.append(0)
else:
disp.append(disp[i-1]+recvcount[i-1])
again = 1
total_iter = 0
cs = [0.0]*2
delta = 0.0
#----------------------------------[ gpu setup ]
print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: MPI proc["+str(myid)+"] run pre-alignment GPU setup" )
# alignment parameters
aln_cfg = AlignConfig(
len(data), 1, # no. of images & averages
data[0].get_xsize(), # image size (images are always assumed to be square)
numr[-3], 256, # polar sampling params (no. of rings, no. of sample on ring)
step[0], xrng[0], xrng[0]) # shift params (step size & shift range in x/y dim)
aln_cfg.freeze()
# find largest batch size we can fit on the given card
gpu_batch_limit = 0
for split in [ 2**i for i in range(int(math.log(len(data),2))+1) ][::-1]:
aln_cfg.sbj_num = min( gpu_batch_limit + split, len(data) )
if cu_module.pre_align_size_check( ctypes.c_uint(len(data)), ctypes.byref(aln_cfg), ctypes.c_uint(myid), ctypes.c_float(cuda_device_occ), ctypes.c_bool(False) ) == True:
gpu_batch_limit += split
gpu_batch_limit = aln_cfg.sbj_num
if gpu_batch_limit > 60000:
gpu_batch_limit = 60000
aln_cfg.sbj_num = gpu_batch_limit
print(gpu_batch_limit)
print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: GPU["+str(myid)+"] pre-alignment batch size: %d/%d" % (gpu_batch_limit, len(data)) )
# initialize gpu resources (returns location for our alignment parameters in CUDA unified memory)
gpu_aln_param = cu_module.pre_align_init( ctypes.c_uint(len(data)), ctypes.byref(aln_cfg), ctypes.c_uint(myid) )
gpu_aln_param = ctypes.cast( gpu_aln_param, aln_param_ptr ) # cast to allow Python-side r/w access
gpu_batch_count = len(data)/gpu_batch_limit if len(data)%gpu_batch_limit==0 else len(data)//gpu_batch_limit+1
print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: GPU["+str(myid)+"] batch count:", gpu_batch_count )
# if the local stack fits on the gpu we only fetch the img data once before we loop
if( gpu_batch_count == 1 ):
cu_module.pre_align_fetch(
get_c_ptr_array(data),
ctypes.c_uint(len(data)),
ctypes.c_char_p("sbj_batch") )
#----------------------------------[ alignment ]
print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: MPI proc["+str(myid)+"] start alignment iterations" )
N_step = 0
# set gpu search parameters
cu_module.reset_shifts( ctypes.c_float(xrng[N_step]), ctypes.c_float(step[N_step]) )
gpu_ref_sum = cp.zeros((1, 2, nx, nx), cp.float32)
for Iter in range(max_iter):
total_iter += 1
#------------------------------------[ construct new average ]
if Iter ==0:
# sum up the original images
ave1, ave2 = statistics.sum_oe( data, "a", CTF, EMData() ) # rotation here, pass empty object to prevent calculation of ctf^2
else:
ref_sum = cp.asnumpy(gpu_ref_sum)
ave1 = EMNumPy.numpy2em(ref_sum[0][0])
ave2 = EMNumPy.numpy2em(ref_sum[0][1])
util.reduce_EMData_to_root(ave1, myid, main_node, comm=mpi_comm) # sum_oe returns sum of [o]dd and [e]ven items
util.reduce_EMData_to_root(ave2, myid, main_node, comm=mpi_comm)
if myid == main_node:
# log iteration info
log.add("Iteration #%4d"%(total_iter))
msg = "X range = %5.2f Y range = %5.2f Step = %5.2f" % (xrng[N_step], yrng[N_step], step[N_step])
log.add(msg)
tavg = (ave1+ave2)/total_nima
if outdir:
tavg.write_image(os.path.join(outdir, "aqc.hdf"), total_iter-1)
frsc = statistics.fsc_mask(ave1, ave2, mask, 1.0, os.path.join(outdir, "resolution%03d"%(total_iter)))
else:
frsc = statistics.fsc_mask(ave1, ave2, mask, 1.0)
else: tavg = util.model_blank(nx, nx)
del ave1, ave2
# main node applies (optional) fourier transform and user function (center and filtering)
if myid == main_node:
# a0 should increase; stop algorithm when it decreases.
# However, the result will depend on filtration, so it is not quite right.
# moved it here, so it is for unfiltered average and thus hopefully makes more sense
a1 = tavg.cmp("dot", tavg, dict(negative=0, mask=ref_data[0]) )
msg = "Criterion %d = %15.8e"%(total_iter, a1)
log.add(msg)
ref_data[2] = tavg
ref_data[3] = frsc
# centering (default: average centering method)
if center == -1:
ref_data[1] = 0
tavg, cs = user_func(ref_data)
cs[0] = float(sx_sum)/total_nima
cs[1] = float(sy_sum)/total_nima
tavg = fundamentals.fshift(tavg, -cs[0], -cs[1])
msg = "Average center x = %10.3f Center y = %10.3f"%(cs[0], cs[1])
log.add(msg)
else:
if delta != 0.0:
cnt = ref_data[1]
ref_data[1] = 0
tavg, cs = user_func(ref_data)
if delta != 0.0:
ref_data[1] = cnt
# write the current filtered average
if outdir:
tavg.write_image(os.path.join(outdir, "aqf.hdf"), total_iter-1)
# update abort criterion
if a1 < a0:
if auto_stop:
again = 0
else:
a0 = a1
else:
tavg = util.model_blank(nx, nx)
cs = [0.0]*2
# check for abort
if auto_stop:
again = mpi.mpi_bcast(again, 1, mpi.MPI_INT, main_node, mpi_comm)
#------------------------------------[ alignment ]
# broadcast the newly constructed average to everyone
util.bcast_EMData_to_all(tavg, myid, main_node, comm=mpi_comm)
cs = mpi.mpi_bcast(cs, 2, mpi.MPI_FLOAT, main_node, mpi_comm)
cs = list(map(float, cs))
# backup last iteration's alignment parameters
old_ali_params = []
for im in range(nima):
alpha, sx, sy, mirror, scale = util.get_params2D(data[im])
old_ali_params.extend([alpha, sx, sy, mirror])
####################################### GPU
#"""
# transfer latest average to gpu
cu_module.pre_align_fetch(
get_c_ptr_array([tavg]),
ctypes.c_int(1),
ctypes.c_char_p("ref_batch") ) # NOTE: happens for each shift change
# FOR gpu_batch_i DO ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ >
gpu_ref_sum = cp.zeros((1, 2, nx, nx), cp.float32)
for gpu_batch_idx in range(gpu_batch_count):
# determine next gpu batch and shove to the gpu (NOTE: if we
# only have a single batch, this already happened earlier)
if( gpu_batch_count > 1 ):
gpu_batch_start = gpu_batch_idx * gpu_batch_limit
gpu_batch_end = gpu_batch_start + gpu_batch_limit
if gpu_batch_end > len(data): gpu_batch_end = len(data)
cu_module.pre_align_fetch(
get_c_ptr_array( data[gpu_batch_start:gpu_batch_end] ),
ctypes.c_int( gpu_batch_end-gpu_batch_start ),
ctypes.c_char_p("sbj_batch") )
else:
gpu_batch_start = 0
gpu_batch_end = len(data)
# run the alignment on gpu
gpu_img_ptr = cu_module.pre_align_run_m( ctypes.c_int(gpu_batch_start), ctypes.c_int(gpu_batch_end) )
#print(gpu_batch_end - gpu_batch_start)
gpu_refs = cp.zeros(int(gpu_batch_end - gpu_batch_start))
print(int(gpu_batch_end-gpu_batch_start))
gpu_ref_sum += kernel_sum_oe(gpu_img_ptr, int(gpu_batch_end-gpu_batch_start), nx, gpu_refs, 1)
# print progress bar
gpu_calls_ttl = 1 * max_iter * gpu_batch_count
#gpu_calls_ttl = len(xrng) * max_iter * gpu_batch_count - 1
gpu_calls_cnt = N_step*max_iter*gpu_batch_count + Iter*gpu_batch_count + gpu_batch_idx
gpu_calls_prc = int( float(gpu_calls_cnt+1)/gpu_calls_ttl * 50.0 )
sys.stdout.write( "\r[PRE-ALIGN][GPU"+str(myid)+"][" + "="*gpu_calls_prc + "-"*(50-gpu_calls_prc) + "]~[%d/%d]~[%.2f%%]" % (gpu_calls_cnt+1, gpu_calls_ttl, (float(gpu_calls_cnt+1)/gpu_calls_ttl)*100.0) )
sys.stdout.flush()
if gpu_calls_cnt+1 == gpu_calls_ttl: print("")
# < ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ FOR gpu_batch_i DO
# convert alignment parameters to the format expected by
# other sphire functions and update EMData headers
sx_sum, sy_sum = 0.0, 0.0
for k, img in enumerate(data):
# this is usually done in ormq()
angle = gpu_aln_param[k].angle
sx_neg = -gpu_aln_param[k].shift_x
sy_neg = -gpu_aln_param[k].shift_y
c_ang = math.cos( math.radians(angle) )
s_ang = -math.sin( math.radians(angle) )
shift_x = sx_neg*c_ang - sy_neg*s_ang
shift_y = sx_neg*s_ang + sy_neg*c_ang
if gpu_aln_param[k].mirror==0:
sx_sum += shift_x
else:
sx_sum -= shift_x
sy_sum += shift_y
sx_sum = mpi.mpi_reduce( sx_sum, 1, mpi.MPI_FLOAT, mpi.MPI_SUM, main_node, mpi_comm )
sy_sum = mpi.mpi_reduce( sy_sum, 1, mpi.MPI_FLOAT, mpi.MPI_SUM, main_node, mpi_comm )
#"""
###########################################
####################################### GPU
# for SHC
if random_method == "SHC":
nope = mpi_reduce(nope, 1, mpi.MPI_INT, mpi.MPI_SUM, main_node, mpi_comm)
nope = mpi_bcast (nope, 1, mpi.MPI_INT, main_node, mpi_comm)
pixel_error = 0.0
mirror_consistent = 0
pixel_error_list = [-1.0]*nima
for im in range(nima):
alpha, sx, sy, mirror, scale = util.get_params2D(data[im])
if old_ali_params[im*4+3] == mirror:
this_error = pixel_error_2D(old_ali_params[im*4:im*4+3], [alpha, sx, sy], last_ring)
pixel_error += this_error
pixel_error_list[im] = this_error
mirror_consistent += 1
del old_ali_params
mirror_consistent = mpi.mpi_reduce(mirror_consistent, 1, mpi.MPI_INT, mpi.MPI_SUM, main_node, mpi_comm)
pixel_error = mpi.mpi_reduce(pixel_error, 1, mpi.MPI_FLOAT, mpi.MPI_SUM, main_node, mpi_comm)
pixel_error_list = mpi.mpi_gatherv(pixel_error_list, nima, mpi.MPI_FLOAT, recvcount, disp, mpi.MPI_FLOAT, main_node, mpi_comm)
if myid == main_node and outdir:
util.drop_image(tavg, os.path.join(outdir, "aqfinal.hdf"))
mpi.mpi_barrier(mpi_comm)
for k, img in enumerate(data):
# this is usually done in ormq()
angle = gpu_aln_param[k].angle
sx_neg = -gpu_aln_param[k].shift_x
sy_neg = -gpu_aln_param[k].shift_y
c_ang = math.cos( math.radians(angle) )
s_ang = -math.sin( math.radians(angle) )
shift_x = sx_neg*c_ang - sy_neg*s_ang
shift_y = sx_neg*s_ang + sy_neg*c_ang
# this happens in ali2d_single_iter()
util.set_params2D( img, [angle, shift_x, shift_y, gpu_aln_param[k].mirror, 1.0], "xform.align2d" )
# free gpu resources
cu_module.gpu_clear()
mpi.mpi_barrier(mpi_comm)
params = []
for im in range(nima):
alpha, sx, sy, mirror, scale = util.get_params2D(data[im])
params.append([alpha, sx, sy, mirror])
mpi.mpi_barrier(mpi_comm)
tmp = params[:]
tmp = spx.wrap_mpi_gatherv(tmp, main_node, mpi_comm)
if( myid == main_node ):
spx.write_text_row( tmp, os.path.join(outdir,"initial2Dparams.txt") )
del tmp
if myid == main_node:
log.add("Finished ali2d_base")
if myid==0: print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base_gpu_isac_CLEAN() :: Alignment complete" )
return params
def ali2d_base(stack, outdir, maskfile=None, ir=1, ou=-1, rs=1, xr="4 2 1 1", yr="-1", ts="2 1 0.5 0.25", \
nomirror = False, dst=0.0, center=-1, maxit=0, CTF=False, snr=1.0, \
Fourvar=False, user_func_name="ref_ali2d", random_method = "", log = None, \
number_of_proc = 1, myid = 0, main_node = 0, mpi_comm = None, write_headers = False):
from sp_utilities import model_circle, model_blank, drop_image, get_image, get_input_from_string
from sp_utilities import reduce_EMData_to_root, bcast_EMData_to_all, send_attr_dict, file_type
from sp_utilities import bcast_number_to_all, bcast_list_to_all
from sp_statistics import fsc_mask, sum_oe, hist_list, varf2d_MPI
from sp_alignment import Numrinit, ringwe, ali2d_single_iter
from sp_pixel_error import pixel_error_2D
from numpy import reshape, shape
from sp_fundamentals import fshift, fft, rot_avg_table
from sp_utilities import get_params2D, set_params2D
from sp_utilities import wrap_mpi_gatherv
import os
import sys
from sp_applications import MPI_start_end
from mpi import mpi_init, mpi_comm_size, mpi_comm_rank, MPI_COMM_WORLD
from mpi import mpi_reduce, mpi_bcast, mpi_barrier, mpi_gatherv
from mpi import MPI_SUM, MPI_FLOAT, MPI_INT
import time
if log == None:
from sp_logger import Logger
log = Logger()
if mpi_comm == None:
mpi_comm = MPI_COMM_WORLD
# ftp = file_type(stack)
if myid == main_node:
import sp_global_def
sp_global_def.LOGFILE = os.path.join(outdir, sp_global_def.LOGFILE)
log.add("Start ali2d_MPI")
xrng = get_input_from_string(xr)
if yr == "-1": yrng = xrng
else : yrng = get_input_from_string(yr)
step = get_input_from_string(ts)
first_ring=int(ir); last_ring=int(ou); rstep=int(rs); max_iter=int(maxit);
if max_iter == 0:
max_iter = 10
auto_stop = True
else:
auto_stop = False
import types
if( type(stack) is bytes ):
if myid == main_node:
total_nima = EMUtil.get_image_count(stack)
else:
total_nima = 0
total_nima = bcast_number_to_all(total_nima)
list_of_particles = list(range(total_nima))
image_start, image_end = MPI_start_end(total_nima, number_of_proc, myid)
list_of_particles = list_of_particles[image_start:image_end]
nima = len(list_of_particles)
data = EMData.read_images(stack, list_of_particles)
else:
data = stack
total_nima = len(data)
total_nima = mpi_reduce(total_nima, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD)
total_nima = mpi_bcast(total_nima, 1, MPI_INT, main_node, MPI_COMM_WORLD)[0]
list_of_particles = list(range(total_nima))
image_start, image_end = MPI_start_end(total_nima, number_of_proc, myid)
list_of_particles = list_of_particles[image_start:image_end]
nima = len(list_of_particles)
assert( nima == len(data))
# read nx and ctf_app (if CTF) and broadcast to all nodes
if myid == main_node:
nx = data[0].get_xsize()
else:
nx = 0
nx = bcast_number_to_all(nx, source_node = main_node)
phase_flip = False
CTF = False
# default value for the last ring
if last_ring == -1: last_ring = nx/2-2
if last_ring + max([max(xrng), max(yrng)]) > (nx-1) // 2:
ERROR('Shift or radius is too large - particle crosses image boundary', "ali2d_MPI", 1)
if myid == main_node:
# log.add("Input stack : %s"%(stack))
log.add("Number of images : %d"%(total_nima))
log.add("Output directory : %s"%(outdir))
log.add("Inner radius : %i"%(first_ring))
log.add("Outer radius : %i"%(last_ring))
log.add("Ring step : %i"%(rstep))
log.add("X search range : %s"%(xrng))
log.add("Y search range : %s"%(yrng))
log.add("Translational step : %s"%(step))
log.add("Disable checking mirror : %s"%(nomirror))
log.add("Discrete angle used : %d"%(dst))
log.add("Center type : %i"%(center))
log.add("Maximum iteration : %i"%(max_iter))
#log.add("Use Fourier variance : %s\n"%(Fourvar))
log.add("CTF correction : %s"%(CTF))
log.add("Phase flip : %s"%(phase_flip))
#log.add("Signal-to-Noise Ratio : %f\n"%(snr))
if auto_stop:
log.add("Stop iteration with : criterion")
else:
log.add("Stop iteration with : maxit")
import sp_user_functions
user_func = sp_user_functions.factory[user_func_name]
log.add("User function : %s"%(user_func_name))
log.add("Number of processors used : %d"%(number_of_proc))
if maskfile:
import types
if type(maskfile) is bytes:
if myid == main_node: log.add("Maskfile : %s"%(maskfile))
mask = get_image(maskfile)
else:
if myid == main_node: log.add("Maskfile : user provided in-core mask")
mask = maskfile
else:
if myid == main_node: log.add("Maskfile : default, a circle with radius %i"%(last_ring))
mask = model_circle(last_ring, nx, nx)
cnx = nx/2+1
cny = cnx
if random_method == "SCF": mode = "H"
else: mode = "F"
ctf_2_sum = None
for im in range(nima):
data[im].set_attr('ID', list_of_particles[im])
set_params2D(data[im], [0.0, 0.0, 0.0, 0, 1.0], 'xform.align2d')
st = Util.infomask(data[im], mask, False)
data[im] -= st[0]
if( random_method == "SHC" ): data[im].set_attr('previousmax',1.0e-23)
ctf_2_sum = None
# startup
numr = Numrinit(first_ring, last_ring, rstep, mode) #precalculate rings
wr = ringwe(numr, mode)
if myid == main_node:
# initialize data for the reference preparation function
ref_data = [mask, center, None, None]
sx_sum = 0.0
sy_sum = 0.0
a0 = -1.0e22
recvcount = []
disp = []
for i in range(number_of_proc):
ib, ie = MPI_start_end(total_nima, number_of_proc, i)
recvcount.append(ie-ib)
if i == 0:
disp.append(0)
else:
disp.append(disp[i-1]+recvcount[i-1])
N_step = 0 #only test first
again = 1
total_iter = 0
cs = [0.0]*2
delta = 0.0
if myid == main_node: print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base() start alignment iterations" )
for Iter in range(max_iter):
total_iter += 1
ave1, ave2 = sum_oe(data, "a", CTF, EMData()) # pass empty object to prevent calculation of ctf^2
reduce_EMData_to_root(ave1, myid, main_node)
reduce_EMData_to_root(ave2, myid, main_node)
sys.stdout.flush()
if myid == main_node:
log.add("Iteration #%4d"%(total_iter))
msg = "X range = %5.2f Y range = %5.2f Step = %5.2f"%(xrng[N_step], yrng[N_step], step[N_step])
log.add(msg)
tavg = (ave1+ave2)/total_nima
if outdir:
tavg.write_image(os.path.join(outdir, "aqc.hdf"), total_iter-1)
frsc = fsc_mask(ave1, ave2, mask, 1.0, os.path.join(outdir, "resolution%03d"%(total_iter)))
else:
frsc = fsc_mask(ave1, ave2, mask, 1.0)
else:
tavg = model_blank(nx, nx)
del ave1, ave2
if Fourvar:
bcast_EMData_to_all(tavg, myid, main_node)
vav, rvar = varf2d_MPI(myid, data, tavg, mask, "a", CTF)
if myid == main_node:
if Fourvar:
tavg = fft(Util.divn_img(fft(tavg), vav))
vav_r = Util.pack_complex_to_real(vav)
if outdir:
vav_r.write_image(os.path.join(outdir, "varf.hdf"), total_iter-1)
# a0 should increase; stop algorithm when it decreases.
# However, the result will depend on filtration, so it is not quite right.
# moved it here, so it is for unfiltered average and thus hopefully makes more sense
a1 = tavg.cmp("dot", tavg, dict(negative = 0, mask = ref_data[0]))
msg = "Criterion %d = %15.8e"%(total_iter, a1)
log.add(msg)
ref_data[2] = tavg
ref_data[3] = frsc
# call user-supplied function to prepare reference image, i.e., center and filter it
if center == -1:
# When center = -1, which is by default, we use the average center method
ref_data[1] = 0
tavg, cs = user_func(ref_data)
cs[0] = float(sx_sum)/total_nima
cs[1] = float(sy_sum)/total_nima
tavg = fshift(tavg, -cs[0], -cs[1])
msg = "Average center x = %10.3f Center y = %10.3f"%(cs[0], cs[1])
log.add(msg)
else:
if delta != 0.0:
cnt = ref_data[1]
ref_data[1] = 0
tavg, cs = user_func(ref_data)
if delta != 0.0:
ref_data[1] = cnt
# write the current filtered average
if outdir:
tavg.write_image(os.path.join(outdir, "aqf.hdf"), total_iter-1)
if a1 < a0:
if auto_stop: again = 0
else: a0 = a1
else:
tavg = model_blank(nx, nx)
cs = [0.0]*2
if auto_stop:
again = mpi_bcast(again, 1, MPI_INT, main_node, mpi_comm)
if Fourvar: del vav
bcast_EMData_to_all(tavg, myid, main_node)
cs = mpi_bcast(cs, 2, MPI_FLOAT, main_node, mpi_comm)
cs = list(map(float, cs))
if total_iter != max_iter*len(xrng):
old_ali_params = []
for im in range(nima):
alpha, sx, sy, mirror, scale = get_params2D(data[im])
old_ali_params.extend([alpha, sx, sy, mirror])
if Iter%4 != 0 or total_iter > max_iter*len(xrng)-10: delta = 0.0
else: delta = dst
sx_sum, sy_sum, nope = ali2d_single_iter(data, numr, wr, cs, tavg, cnx, cny, \
xrng[N_step], yrng[N_step], step[N_step], \
nomirror=nomirror, mode=mode, CTF=CTF, delta=delta, \
random_method = random_method)
sx_sum = mpi_reduce(sx_sum, 1, MPI_FLOAT, MPI_SUM, main_node, mpi_comm)
sy_sum = mpi_reduce(sy_sum, 1, MPI_FLOAT, MPI_SUM, main_node, mpi_comm)
# for SHC
if random_method == "SHC":
nope = mpi_reduce(nope, 1, MPI_INT, MPI_SUM, main_node, mpi_comm)
nope = mpi_bcast(nope, 1, MPI_INT, main_node, mpi_comm)
pixel_error = 0.0
mirror_consistent = 0
pixel_error_list = [-1.0]*nima
for im in range(nima):
alpha, sx, sy, mirror, scale = get_params2D(data[im])
if old_ali_params[im*4+3] == mirror:
this_error = pixel_error_2D(old_ali_params[im*4:im*4+3], [alpha, sx, sy], last_ring)
pixel_error += this_error
pixel_error_list[im] = this_error
mirror_consistent += 1
del old_ali_params
mirror_consistent = mpi_reduce(mirror_consistent, 1, MPI_INT, MPI_SUM, main_node, mpi_comm)
pixel_error = mpi_reduce(pixel_error, 1, MPI_FLOAT, MPI_SUM, main_node, mpi_comm)
pixel_error_list = mpi_gatherv(pixel_error_list, nima, MPI_FLOAT, recvcount, disp, MPI_FLOAT, main_node, mpi_comm)
if myid == main_node:
log.add("Mirror consistency rate = %8.4f%%"%(float(mirror_consistent)/total_nima*100))
if mirror_consistent!=0:
log.add("Among the mirror-consistent images, average of pixel errors is %0.4f, and their distribution is:"%(float(pixel_error)/float(mirror_consistent)))
pixel_error_list = list(map(float, pixel_error_list))
for i in range(total_nima-1, -1, -1):
if pixel_error_list[i] < 0: del pixel_error_list[i]
region, hist = hist_list(pixel_error_list, 20)
for p in range(20):
log.add(" %14.2f: %6d"%(region[p], hist[p]))
log.add("\n\n")
if myid == main_node and outdir: drop_image(tavg, os.path.join(outdir, "aqfinal.hdf"))
if myid == main_node: print( time.strftime("%Y-%m-%d %H:%M:%S :: ", time.localtime()) + "ali2d_base() end alignment iterations" )
# write out headers and STOP, under MPI writing has to be done sequentially
mpi_barrier(mpi_comm)
params = []
for im in range(nima):
alpha, sx, sy, mirror, scale = get_params2D(data[im])
params.append([alpha, sx, sy, mirror])
mpi_barrier(mpi_comm)
tmp = params[:]
tmp = spx.wrap_mpi_gatherv(tmp, main_node, mpi_comm)
if( myid == main_node ):
spx.write_text_row( tmp, os.path.join(outdir,"initial2Dparams.txt") )
del tmp
if myid == main_node: log.add("Finished ali2d_base")
return params #, data
def mpi_assert( condition, msg ):
if not condition:
mpi_rank = mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD)
print( "MPI PROC["+str(mpi_rank)+"] ASSERTION ERROR:", msg, file=sys.stderr)
sys.stderr.flush()
mpi.mpi_finalize()
sys.exit()
def main():
progname = os.path.basename(sys.argv[0])
usage = progname + " stack outdir <maskfile> --ir=inner_radius --ou=outer_radius --rs=ring_step --xr=x_range --yr=y_range --ts=translation_step --dst=delta --center=center --maxit=max_iteration --CTF --snr=SNR --Fourvar=Fourier_variance --Ng=group_number --Function=user_function_name --CUDA --GPUID --MPI"
parser = OptionParser(usage,version=SPARXVERSION)
parser.add_option("--ir", type="float", default=1, help="inner radius for rotational correlation > 0 (set to 1)")
parser.add_option("--ou", type="float", default=-1, help="outer radius for rotational correlation < nx/2-1 (set to the radius of the particle)")
parser.add_option("--rs", type="float", default=1, help="step between rings in rotational correlation > 0 (set to 1)" )
parser.add_option("--xr", type="string", default="4 2 1 1", help="range for translation search in x direction, search is +/xr ")
parser.add_option("--yr", type="string", default="-1", help="range for translation search in y direction, search is +/yr ")
parser.add_option("--ts", type="string", default="2 1 0.5 0.25",help="step of translation search in both directions")
parser.add_option("--nomirror", action="store_true", default=False, help="Disable checking mirror orientations of images (default False)")
parser.add_option("--dst", type="float", default=0.0, help="delta")
parser.add_option("--center", type="float", default=-1, help="-1.average center method; 0.not centered; 1.phase approximation; 2.cc with Gaussian function; 3.cc with donut-shaped image 4.cc with user-defined reference 5.cc with self-rotated average")
parser.add_option("--maxit", type="float", default=0, help="maximum number of iterations (0 means the maximum iterations is 10, but it will automatically stop should the criterion falls")
parser.add_option("--CTF", action="store_true", default=False, help="use CTF correction during alignment")
parser.add_option("--snr", type="float", default=1.0, help="signal-to-noise ratio of the data (set to 1.0)")
parser.add_option("--Fourvar", action="store_true", default=False, help="compute Fourier variance")
parser.add_option("--function", type="string", default="ref_ali2d", help="name of the reference preparation function (default ref_ali2d)")
parser.add_option("--gpu_devices", type="string", default="", help="Specify the GPUs to be used (e.g. --gpu_devices=0, or --gpu_devices=0,1 for one or two GPUs, respectively). Using \"$ nividia-smi\" in the terminal will print out what GPUs are available. [Default: None]" )
parser.add_option( "--gpu_info", action="store_true", default=False, help="Print detailed information about the selected GPUs. Use --gpu_devices to specify what GPUs you want to know about. NOTE: program will stop after printing this information, so don't use this parameter if you intend to actually process any data. [Default: False]" )
parser.add_option("--MPI", action="store_true", default=False, help="use MPI version ")
parser.add_option("--mode", type="string", default="F", help="Full or Half rings, default F")
parser.add_option("--randomize",action="store_true", default=False, help="randomize initial rotations (suboption of friedel, default False)")
parser.add_option("--orient", action="store_true", default=False, help="orient images such that the average is symmetric about x-axis, for layer lines (suboption of friedel, default False)")
parser.add_option("--random_method", type="string", default="", help="use SHC or SCF (default standard method)")
(options, args) = parser.parse_args()
if len(args) < 2 or len(args) > 3:
print("usage: " + usage)
print("Please run '" + progname + " -h' for detailed options")
else:
if args[1] == 'None': outdir = None
else: outdir = args[1]
if len(args) == 2: mask = None
else: mask = args[2]
if global_def.CACHE_DISABLE:
from utilities import disable_bdb_cache
disable_bdb_cache()
global_def.BATCH = True
if options.MPI:
from mpi import mpi_init, mpi_comm_size, mpi_comm_rank, MPI_COMM_WORLD
import mpi
sys.argv = mpi_init(len(sys.argv),sys.argv) # init and finalize are needed for each process
number_of_proc = mpi_comm_size(MPI_COMM_WORLD) # read from mpirun
myid = mpi_comm_rank(MPI_COMM_WORLD)
main_node = 0
if(myid == main_node):
import subprocess
from logger import Logger, BaseLogger_Files
# Create output directory
log = Logger(BaseLogger_Files())
log.prefix = os.path.join(outdir)
cmd = "mkdir "+log.prefix
outcome = subprocess.call(cmd, shell=True)
log.prefix += "/"
else:
outcome = 0
log = None
from utilities import bcast_number_to_all
outcome = bcast_number_to_all(outcome, source_node = main_node)
if(outcome == 1):
ERROR('Output directory exists, please change the name and restart the program', "ali2d_MPI", 1, myid)
params2d = ali2d_base(args[0], outdir, mask, options.ir, options.ou, options.rs, options.xr, options.yr, \
options.ts, options.nomirror, options.dst, \
options.center, options.maxit, options.CTF, options.snr, options.Fourvar, \
options.function, random_method = options.random_method, log = log, \
number_of_proc = number_of_proc, myid = myid, main_node = main_node, mpi_comm = MPI_COMM_WORLD,\
write_headers = True)
else:
import time
from mpi import mpi_init, mpi_comm_size, mpi_comm_rank, MPI_COMM_WORLD
import mpi
sys.argv = mpi_init(len(sys.argv),sys.argv)
global Blockdata
Blockdata = {}
Blockdata["nproc"] = mpi_comm_size(mpi.MPI_COMM_WORLD)
myid = mpi_comm_rank(MPI_COMM_WORLD)
main_node = 0
### GPU Related ###
Blockdata["shared_comm"] = mpi.mpi_comm_split_type(mpi.MPI_COMM_WORLD, mpi.MPI_COMM_TYPE_SHARED, 0, mpi.MPI_INFO_NULL)
Blockdata["myid_on_node"] = mpi.mpi_comm_rank(Blockdata["shared_comm"])