Changeset c036ddb in sasmodels for sasmodels/kernelcl.py


Ignore:
Timestamp:
Aug 7, 2018 10:45:45 PM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
7e923c2
Parents:
7b0abf8
Message:

refactor so Iq is not needed if Fq is defined

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kernelcl.py

    r01c8d9e rc036ddb  
    420420        if self.program is None: 
    421421            compile_program = environment().compile_program 
    422             with open('model.c','w') as fid: 
    423                 print(self.source['opencl'], file=fid) 
    424422            timestamp = generate.ocl_timestamp(self.info) 
    425423            self.program = compile_program( 
     
    540538        self.dtype = dtype 
    541539        self.dim = '2d' if q_input.is_2d else '1d' 
    542         # plus three for the normalization values 
    543         self.result = np.empty(2*q_input.nq+2,dtype) 
     540        # leave room for f1/f2 results in case we need to compute beta for 1d models 
     541        num_returns = 1 if self.dim == '2d' else 2  # 
     542        # plus 1 for the normalization value 
     543        self.result = np.empty((q_input.nq+1)*num_returns, dtype) 
    544544 
    545545        # Inputs and outputs for each kernel call 
     
    549549 
    550550        self.result_b = cl.Buffer(self.queue.context, mf.READ_WRITE, 
    551                                   q_input.global_size[0] * dtype.itemsize) 
     551                                  q_input.global_size[0] * num_returns * dtype.itemsize) 
    552552        self.q_input = q_input # allocated by GpuInput above 
    553553 
     
    557557                     else np.float16 if dtype == generate.F16 
    558558                     else np.float32)  # will never get here, so use np.float32 
    559     __call__= Iq 
    560559 
    561560    def Iq(self, call_details, values, cutoff, magnetic): 
     561        # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray 
     562        self._call_kernel(call_details, values, cutoff, magnetic) 
     563        #print("returned",self.q_input.q, self.result) 
     564        pd_norm = self.result[self.q_input.nq] 
     565        scale = values[0]/(pd_norm if pd_norm != 0.0 else 1.0) 
     566        background = values[1] 
     567        #print("scale",scale,background) 
     568        return scale*self.result[:self.q_input.nq] + background 
     569    __call__ = Iq 
     570 
     571    def beta(self, call_details, values, cutoff, magnetic): 
     572        # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray 
     573        if self.dim == '2d': 
     574            raise NotImplementedError("beta not yet supported for 2D") 
     575        self._call_kernel(call_details, values, cutoff, magnetic) 
     576        w_norm = self.result[2*self.q_input.nq + 1] 
     577        pd_norm = self.result[self.q_input.nq] 
     578        if w_norm == 0.: 
     579            w_norm = 1. 
     580        F2 = self.result[:self.q_input.nq]/w_norm 
     581        F1 = self.result[self.q_input.nq+1:2*self.q_input.nq+1]/w_norm 
     582        volume_avg = pd_norm/w_norm 
     583        return F1, F2, volume_avg 
     584 
     585    def _call_kernel(self, call_details, values, cutoff, magnetic): 
    562586        # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray 
    563587        context = self.queue.context 
     
    575599        ] 
    576600        #print("Calling OpenCL") 
    577         call_details.show(values) 
     601        #call_details.show(values) 
    578602        #Call kernel and retrieve results 
    579603        wait_for = None 
     
    601625                v.release() 
    602626 
    603         pd_norm = self.result[self.q_input.nq] 
    604         scale = values[0]/(pd_norm if pd_norm != 0.0 else 1.0) 
    605         background = values[1] 
    606         #print("scale",scale,values[0],self.result[self.q_input.nq],background) 
    607         return scale*self.result[:self.q_input.nq] + background 
    608         # return self.result[:self.q_input.nq] 
    609      #NEEDS TO BE FINISHED FOR OPENCL 
    610      def beta(): 
    611          return 0 
    612  
    613627    def release(self): 
    614628        # type: () -> None 
Note: See TracChangeset for help on using the changeset viewer.