Changeset 962


Ignore:
Timestamp:
Jun 20, 2013 4:05:55 PM (8 years ago)
Author:
vondreele
Message:

implement multiprocessing for MC/SA calculations

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIgrid.py

    r951 r962  
    5959[ wxID_FOURCALC, wxID_FOURSEARCH, wxID_FOURCLEAR, wxID_PEAKSMOVE, wxID_PEAKSCLEAR,
    6060    wxID_CHARGEFLIP, wxID_PEAKSUNIQUE, wxID_PEAKSDELETE, wxID_PEAKSDA,
    61     wxID_PEAKSDISTVP, wxID_PEAKSVIEWPT, wxID_FINDEQVPEAKS,wxID_SHOWBONDS,wxID_RUNMCSA,
    62 ] = [wx.NewId() for item in range(14)]
     61    wxID_PEAKSDISTVP, wxID_PEAKSVIEWPT, wxID_FINDEQVPEAKS,wxID_SHOWBONDS,wxID_MULTIMCSA,
     62    wxID_SINGLEMCSA
     63] = [wx.NewId() for item in range(15)]
    6364
    6465[ wxID_PWDRADD, wxID_HKLFADD,wxID_PWDANALYSIS,wxID_DATADELETE,
     
    17411742        self.GeneralCalc.Append(help='Clear map',id=wxID_FOURCLEAR, kind=wx.ITEM_NORMAL,
    17421743            text='Clear map')
    1743         self.GeneralCalc.Append(help='Run Monte Carlo - Simulated Annealing',id=wxID_RUNMCSA, kind=wx.ITEM_NORMAL,
    1744             text='MC/SA')
     1744        self.GeneralCalc.Append(help='Run Monte Carlo - Simulated Annealing on single processor',id=wxID_SINGLEMCSA, kind=wx.ITEM_NORMAL,
     1745            text='Single MC/SA')
     1746        self.GeneralCalc.Append(help='Run Monte Carlo - Simulated Annealing on multiprocessors',id=wxID_MULTIMCSA, kind=wx.ITEM_NORMAL,
     1747            text='Multi MC/SA')
    17451748        self.PostfillDataMenu()
    17461749       
  • trunk/GSASIImath.py

    r961 r962  
    23172317        x0 = schedule.getstart_temp(best_state)
    23182318    else:
     2319        x0 = random.uniform(size=len(x0))*(upper-lower) + lower
    23192320        best_state.x = None
    23202321        best_state.cost = numpy.Inf
     
    23472348                newmsg='%s%8.5f\n%s%8.4f%s'%('Temperature =',schedule.T,'MC/SA Residual =',best_state.cost*100,'%'))[0]
    23482349            if not GoOn:
     2350                best_state.x = last_state.x.copy()
     2351                best_state.cost = last_state.cost
    23492352                break
    23502353        schedule.update_temp()
     
    23892392        return best_state.x, retval
    23902393
     2394def worker(iCyc,data,RBdata,reflType,reflData,covData,out_q):
     2395    outlist = []
     2396    for n in range(iCyc):
     2397        result = mcsaSearch(data,RBdata,reflType,reflData,covData,None)
     2398        outlist.append(result[0])
     2399        print ' MC/SA residual: %.3f%% structure factor time: %.3f'%(100*result[0][2],result[1])
     2400    out_q.put(outlist)
     2401
     2402def MPmcsaSearch(nCyc,data,RBdata,reflType,reflData,covData):
     2403    import multiprocessing as mp
     2404   
     2405    nprocs = mp.cpu_count()
     2406    out_q = mp.Queue()
     2407    procs = []
     2408    iCyc = np.zeros(nprocs)
     2409    for i in range(nCyc):
     2410        iCyc[i%nprocs] += 1
     2411    for i in range(nprocs):
     2412        p = mp.Process(target=worker,args=(int(iCyc[i]),data,RBdata,reflType,reflData,covData,out_q))
     2413        procs.append(p)
     2414        p.start()
     2415    resultlist = []
     2416    for i in range(nprocs):
     2417        resultlist += out_q.get()
     2418    for p in procs:
     2419        p.join()
     2420       
     2421    return resultlist
     2422   
    23912423
    23922424def mcsaSearch(data,RBdata,reflType,reflData,covData,pgbar):
  • trunk/GSASIIphsGUI.py

    r961 r962  
    726726            line2Sizer = wx.BoxSizer(wx.HORIZONTAL)
    727727            line2Sizer.Add(wx.StaticText(General,label=' MC/SA runs: '),0,wx.ALIGN_CENTER_VERTICAL)
    728             Cchoice = ['1','2','3','5','10','15','20','30']
     728            Cchoice = ['1','2','3','4','8','12','20','32','64','128']
    729729            cycles = wx.ComboBox(General,-1,value=str(MCSA.get('Cycles',1)),choices=Cchoice,
    730730                style=wx.CB_READONLY|wx.CB_DROPDOWN)
    731731            cycles.Bind(wx.EVT_COMBOBOX,OnCycles)       
    732732            line2Sizer.Add(cycles,0,wx.ALIGN_CENTER_VERTICAL)
    733             Achoice = ['log','fast','cauchy','boltzmann','Tremayne']
     733            Achoice = ['log','fast']                #these work
     734#            Achoice = ['log','fast','cauchy','boltzmann','Tremayne']
    734735            line2Sizer.Add(wx.StaticText(General,label=' MC/SA schedule: '),0,wx.ALIGN_CENTER_VERTICAL)
    735736            Alist = wx.ComboBox(General,-1,value=MCSA['Algorithm'],choices=Achoice,
     
    43264327            MCSA.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1)
    43274328            G2frame.dataFrame.Update()
    4328 
    4329     def OnRunMCSA(event):
     4329           
     4330    def OnRunMultiMCSA(event):
     4331        RunMCSA('multi')
     4332       
     4333    def OnRunSingleMCSA(event):
     4334        RunMCSA('single')
     4335
     4336    def RunMCSA(process):
    43304337        generalData = data['General']
    43314338        mcsaControls = generalData['MCSA controls']
     
    43614368            print '**** ERROR - no models defined for MC/SA run****'
    43624369            return
    4363         pgbar = wx.ProgressDialog('MC/SA','Residual Rcf =',101.0,
    4364             style = wx.PD_ELAPSED_TIME|wx.PD_AUTO_HIDE|wx.PD_CAN_ABORT)
    4365         screenSize = wx.ClientDisplayRect()
    4366         Size = pgbar.GetSize()
    4367         Size = (int(Size[0]*1.2),Size[1]) # increase size a bit along x
    4368         pgbar.SetPosition(wx.Point(screenSize[2]-Size[0]-305,screenSize[1]+5))
    4369         pgbar.SetSize(Size)
     4370        if process == 'single':
     4371            pgbar = wx.ProgressDialog('MC/SA','Residual Rcf =',101.0,
     4372                style = wx.PD_ELAPSED_TIME|wx.PD_AUTO_HIDE|wx.PD_CAN_ABORT)
     4373            screenSize = wx.ClientDisplayRect()
     4374            Size = pgbar.GetSize()
     4375            Size = (int(Size[0]*1.2),Size[1]) # increase size a bit along x
     4376            pgbar.SetPosition(wx.Point(screenSize[2]-Size[0]-305,screenSize[1]+5))
     4377            pgbar.SetSize(Size)
     4378        else:
     4379            pgbar = None
    43704380        time1 = time.time()
    43714381        try:
    43724382            tsf = 0.
    4373             for i in range(mcsaControls['Cycles']):
    4374                 Result,tsum = G2mth.mcsaSearch(data,RBdata,reflType,reflData,covData,pgbar)
    4375                 MCSAdata['Results'].append(Result)
    4376                 print ' MC/SA runs completed: %d residual: %.3f%%'%(i,100*Result[2])
    4377                 tsf += tsum
     4383            nCyc = mcsaControls['Cycles']
     4384            if process == 'single':
     4385                for i in range(nCyc):
     4386                    Result,tsum = G2mth.mcsaSearch(data,RBdata,reflType,reflData,covData,pgbar)
     4387                    MCSAdata['Results'].append(Result)
     4388                    print ' MC/SA run completed: %d residual: %.3f%%'%(i,100*Result[2])
     4389                    tsf += tsum
     4390                print ' Structure factor time: %.2f'%(tsf)
     4391            else:
     4392                MCSAdata['Results'] = G2mth.MPmcsaSearch(nCyc,data,RBdata,reflType,reflData,covData)
    43784393            print ' MC/SA run time: %.2f'%(time.time()-time1)
    4379             print ' Structure factor time: %.2f'%(tsf)
    43804394        finally:
    4381             pgbar.Destroy()
     4395            if process == 'single':
     4396                pgbar.Destroy()
    43824397        MCSAdata['Results'] = G2mth.sortArray(MCSAdata['Results'],2,reverse=False)
    43834398        UpdateMCSA()
     
    50785093    G2frame.dataFrame.Bind(wx.EVT_MENU, OnChargeFlip, id=G2gd.wxID_CHARGEFLIP)
    50795094    G2frame.dataFrame.Bind(wx.EVT_MENU, OnFourClear, id=G2gd.wxID_FOURCLEAR)
    5080     G2frame.dataFrame.Bind(wx.EVT_MENU, OnRunMCSA, id=G2gd.wxID_RUNMCSA)   
     5095    G2frame.dataFrame.Bind(wx.EVT_MENU, OnRunSingleMCSA, id=G2gd.wxID_SINGLEMCSA)   
     5096    G2frame.dataFrame.Bind(wx.EVT_MENU, OnRunMultiMCSA, id=G2gd.wxID_MULTIMCSA)   
    50815097    G2frame.dataDisplay.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, OnPageChanged)
    50825098
Note: See TracChangeset for help on using the changeset viewer.