def get_slp_wall_times(lpot_source, fmm_order, qbx_order): queue = cl.CommandQueue(lpot_source.cl_context) lpot_source = lpot_source.copy( qbx_order=qbx_order, fmm_level_to_order=(False if fmm_order is False else lambda *args: fmm_order)) d = lpot_source.ambient_dim u_sym = sym.var("u") from sumpy.kernel import LaplaceKernel S = sym.S(LaplaceKernel(d), u_sym, qbx_forced_limit=-1) density_discr = lpot_source.density_discr u = cl.array.empty(queue, density_discr.nnodes, np.float64) u.fill(1) op = bind(lpot_source, S) # Warmup op(queue, u=u) times = [] from time import perf_counter as curr_time for _ in range(WALL_TIME_EXPERIMENT_TIMING_ROUNDS): t_start = curr_time() op(queue, u=u) t_end = curr_time() times.append(t_end - t_start) return times
def refresh(self): #response = loads(api.auth_refresh_token(self.refresh_token)) response = { 'access_token': uuid.uuid4(), 'refresh_token': uuid.uuid4(), 'expires_in': 60 } with database.atomic(): self.access_token = response['access_token'] self.refresh_token = response['refresh_token'] self.expires_in = curr_time() + response['expires_in'] self.save() return self
def get_token(self, username, password): #response = loads(api.auth_get_token(username, password)) response = { 'access_token': uuid.uuid4(), 'refresh_token': uuid.uuid4(), 'expires_in': 60 } access_token = response['access_token'] refresh_token = response['refresh_token'] expires_in = response['expires_in'] with database: token = Token.create(user=self, access_token=access_token, refresh_token=refresh_token, token_expires=curr_time() + expires_in) return token
def save(self, commit=True): challenge = self.participation.challenge competition = Competition.objects.filter(challenge=challenge, type='friendly').first() matches = Match.objects.filter(competition=competition, part1__object_id=self.participation.id) if matches.exists(): last_submission = matches.order_by('-time')[0] if datetime.now(utc) - last_submission.time < timedelta( minutes=settings.SINGLE_MATCH_SUBMISSION_TIME_DELTA): raise BaseException("Don't try to fool us") print(self.cleaned_data['battle_team']) if self.cleaned_data['battle_team'] == '-1': teams = [] for team in self.participation.challenge.teams.all(): if team.allow_random and team.submissions.filter( is_final=True).exists(): teams.append(team) teams.sort(key=lambda x: x.team.rate) ind = teams.index(self.participation) st = max(0, ind - settings.RANDOM_MATCH_RANK_RANGE) en = min(ind + settings.RANDOM_MATCH_RANK_RANGE, len(teams)) seed(curr_time()) trial = 0 while trial < 5: trial += 1 second_team_participation = teams[randrange(st, en)] if second_team_participation != self.participation: break approv = False else: second_team_participation = TeamParticipatesChallenge.objects.filter( id=self.cleaned_data['battle_team']).first() approv = True first_participant = Participant() first_participant.depend = self.participation first_participant.submission = self.participation.get_final_submission( ) if commit: first_participant.save() second_participant = Participant() second_participant.depend = second_team_participation second_participant.submission = second_team_participation.get_final_submission( ) if commit: second_participant.save() match = Match(part1=first_participant, part2=second_participant, competition=competition) if commit: match.save() competition_map = Map.objects.filter( id=self.cleaned_data['battle_team_maps']).first() single_match = SingleMatch(match=match, map=competition_map, status='waitacc' if approv else 'waiting') if commit: single_match.save() if not approv: single_match.handle()
def get_green_error(lpot_source, fmm_order, qbx_order, k=0, time=None): queue = cl.CommandQueue(lpot_source.cl_context) lpot_source = lpot_source.copy( qbx_order=qbx_order, fmm_level_to_order=(False if fmm_order is False else lambda *args: fmm_order)) d = lpot_source.ambient_dim u_sym = sym.var("u") dn_u_sym = sym.var("dn_u") from sumpy.kernel import LaplaceKernel, HelmholtzKernel lap_k_sym = LaplaceKernel(d) if k == 0: k_sym = lap_k_sym knl_kwargs = {} else: k_sym = HelmholtzKernel(d) knl_kwargs = {"k": sym.var("k")} S_part = ( # noqa: N806 sym.S(k_sym, dn_u_sym, qbx_forced_limit=-1, **knl_kwargs)) D_part = ( # noqa: N806 sym.D(k_sym, u_sym, qbx_forced_limit="avg", **knl_kwargs)) density_discr = lpot_source.density_discr # {{{ compute values of a solution to the PDE nodes_host = density_discr.nodes().get(queue) normal = bind(density_discr, sym.normal(d))(queue).as_vector(np.object) normal_host = [normal[j].get() for j in range(d)] if k != 0: if d == 2: angle = 0.3 wave_vec = np.array([np.cos(angle), np.sin(angle)]) u = np.exp(1j * k * np.tensordot(wave_vec, nodes_host, axes=1)) grad_u = 1j * k * wave_vec[:, np.newaxis] * u else: center = np.array([3, 1, 2]) diff = nodes_host - center[:, np.newaxis] r = la.norm(diff, axis=0) u = np.exp(1j * k * r) / r grad_u = diff * (1j * k * u / r - u / r**2) else: center = np.array([2, 1, 2])[:d] diff = nodes_host - center[:, np.newaxis] dist_squared = np.sum(diff**2, axis=0) dist = np.sqrt(dist_squared) if d == 2: u = np.log(dist) grad_u = diff / dist_squared elif d == 3: u = 1 / dist grad_u = -diff / dist**3 else: assert False dn_u = 0 for i in range(d): dn_u = dn_u + normal_host[i] * grad_u[i] # }}} u_dev = cl.array.to_device(queue, u) dn_u_dev = cl.array.to_device(queue, dn_u) grad_u_dev = cl.array.to_device(queue, grad_u) bound_S_part = bind(lpot_source, S_part) # noqa: N806 bound_D_part = bind(lpot_source, D_part) # noqa: N806 from time import time as curr_time t_start = curr_time() S_result = bound_S_part( # noqa: N806 queue, u=u_dev, dn_u=dn_u_dev, grad_u=grad_u_dev, k=k) t_end = curr_time() if time is not None: time[0] = t_end - t_start D_result = bound_D_part( # noqa: N806 queue, u=u_dev, dn_u=dn_u_dev, grad_u=grad_u_dev, k=k) scaling_l2 = 1 / norm(density_discr, queue, u_dev, p=2) scaling_linf = 1 / norm(density_discr, queue, u_dev, p="inf") error = S_result - D_result - 0.5 * u_dev return (scaling_l2 * norm(density_discr, queue, error, p=2), scaling_linf * norm(density_discr, queue, error, p="inf"))
def is_expired(self): if curr_time() < self.token_expires: return False else: return True