def test_create_simulate_vis_list(self):
     vis_list = simulate_list_serial_workflow(
         frequency=self.frequency, channel_bandwidth=self.channel_bandwidth)
     assert len(vis_list) == len(self.frequency)
     vt = vis_list[0]
     assert isinstance(vt, BlockVisibility)
     assert vt.nvis > 0
log.info(
    "Starting imaging-pipeline with %d MPI processes nfreqwin %d ntimes %d" %
    (size, nfreqwin, ntimes))
print("Starting imaging-pipeline with %d MPI processes nfreqwin %d ntimes %d" %
      (size, nfreqwin, ntimes),
      flush=True)
log.debug('%d: frequency len %d frequency list:' % (rank, len(frequency)))
#print(frequency,flush=True)

if rank == 0:
    bvis_list = simulate_list_serial_workflow(
        'LOWBD2',
        frequency=frequency,
        channel_bandwidth=channel_bandwidth,
        times=times,
        phasecentre=phasecentre,
        order='frequency',
        rmax=rmax,
        format='blockvis')
else:
    bvis_list = list()

vis_list = [convert_blockvisibility_to_visibility(bv) for bv in bvis_list]
log.debug('%d: %d elements in vis_list' % (rank, len(vis_list)))
#log.handlers[0].flush()
#print(vis_list

# In[4]:

if rank == 0:
Ejemplo n.º 3
0
def trial_case(results,
               seed=180555,
               context='wstack',
               nworkers=8,
               threads_per_worker=1,
               memory=8,
               processes=True,
               order='frequency',
               nfreqwin=7,
               ntimes=3,
               rmax=750.0,
               facets=1,
               wprojection_planes=1,
               use_dask=True,
               use_serial_imaging=True,
               flux_limit=0.3,
               nmajor=5,
               dft_threshold=1.0,
               use_serial_clean=True,
               write_fits=False):
    """ Single trial for performance-timings
    
    Simulates visibilities from GLEAM including phase errors
    Makes dirty image and PSF
    Runs ICAL pipeline
    
    The results are in a dictionary:
    
    'context': input - a string describing concisely the purpose of the test
    'time overall',  overall execution time (s)
    'time predict', time to execute GLEAM prediction graph
    'time invert', time to make dirty image
    'time invert graph', time to make dirty image graph
    'time ICAL graph', time to create ICAL graph
    'time ICAL', time to execute ICAL graph
    'context', type of imaging e.g. 'wstack'
    'nworkers', number of workers to create
    'threads_per_worker',
    'nnodes', Number of nodes,
    'processes', 'order', Ordering of data_models
    'nfreqwin', Number of frequency windows in simulation
    'ntimes', Number of hour angles in simulation
    'rmax', Maximum radius of stations used in simulation (m)
    'facets', Number of facets in deconvolution and imaging
    'wprojection_planes', Number of wprojection planes
    'vis_slices', Number of visibility slices (per Visibbility)
    'npixel', Number of pixels in image
    'cellsize', Cellsize in radians
    'seed', Random number seed
    'dirty_max', Maximum in dirty image
    'dirty_min', Minimum in dirty image
    'restored_max',
    'restored_min',
    'deconvolved_max',
    'deconvolved_min',
    'residual_max',
    'residual_min',
    'git_info', GIT hash (not definitive since local mods are possible)
    
    :param results: Initial state
    :param seed: Random number seed (used in gain simulations)
    :param context: imaging context
    :param context: Type of context: '2d'|'timeslice'|'wstack'
    :param nworkers: Number of dask workers to use
    :param threads_per_worker: Number of threads per worker
    :param processes: Use processes instead of threads 'processes'|'threads'
    :param order: See simulate_list_list_arlexecute_workflow_workflowkflow
    :param nfreqwin: See simulate_list_list_arlexecute_workflow_workflowkflow
    :param ntimes: See simulate_list_list_arlexecute_workflow_workflowkflow
    :param rmax: See simulate_list_list_arlexecute_workflow_workflowkflow
    :param facets: Number of facets to use
    :param wprojection_planes: Number of wprojection planes to use
    :param use_dask: Use dask or immediate evaluation
    :return: results dictionary
    """
    # Initialise MPI

    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    def init_logging():
        logging.basicConfig(
            filename='pipelines_mpi_timings.log',
            filemode='w',
            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
            datefmt='%H:%M:%S',
            level=logging.INFO)

    init_logging()
    log = logging.getLogger()

    # Initialise logging on the workers. This appears to only work using the process scheduler.

    def lprint(*args):
        log.info(*args)
        print(*args)

    lprint("Starting pipelines_mpi_timings")

    numpy.random.seed(seed)
    results['seed'] = seed

    start_all = time.time()

    results['context'] = context
    results['hostname'] = socket.gethostname()
    results['git_hash'] = git_hash()
    results['epoch'] = time.strftime("%Y-%m-%d %H:%M:%S")

    lprint("Context is %s" % context)

    results['nworkers'] = nworkers
    results['threads_per_worker'] = threads_per_worker
    results['processes'] = processes
    results['memory'] = memory
    results['order'] = order
    results['nfreqwin'] = nfreqwin
    results['ntimes'] = ntimes
    results['rmax'] = rmax
    results['facets'] = facets
    results['wprojection_planes'] = wprojection_planes
    results['dft threshold'] = dft_threshold

    results['use_dask'] = use_dask

    lprint("At start, configuration is:")
    lprint(sort_dict(results))

    # Parameters determining scale of simulation.
    frequency = numpy.linspace(1.0e8, 1.2e8, nfreqwin)
    centre = nfreqwin // 2
    if nfreqwin > 1:
        channel_bandwidth = numpy.array(nfreqwin *
                                        [frequency[1] - frequency[0]])
    else:
        channel_bandwidth = numpy.array([1e6])

    times = numpy.linspace(-numpy.pi / 4.0, numpy.pi / 4.0, ntimes)
    phasecentre = SkyCoord(ra=+0.0 * u.deg,
                           dec=-40.0 * u.deg,
                           frame='icrs',
                           equinox='J2000')

    lprint("****** Visibility creation ******")
    # Create the empty BlockVisibility's and persist these on the cluster
    if rank == 0:
        tmp_bvis_list = simulate_list_serial_workflow(
            'LOWBD2',
            frequency=frequency,
            channel_bandwidth=channel_bandwidth,
            times=times,
            phasecentre=phasecentre,
            order=order,
            format='blockvis',
            rmax=rmax)

        vis_list = [
            convert_blockvisibility_to_visibility(bv) for bv in tmp_bvis_list
        ]
    else:
        vis_list = list()

    #if rank==0:
    #    import matplotlib.pyplot as plt
    #    plt.clf()
    #    plt.hist(vis_list[0].w, bins=100)
    #    plt.title('Histogram of w samples: rms=%.1f (wavelengths)' % numpy.std(vis_list[0].w))
    #    plt.xlabel('W (wavelengths)')
    #    plt.show()
    #    plt.clf()
    #    plt.hist(vis_list[0].uvdist, bins=100)
    #    plt.title('Histogram of uvdistance samples')
    #    plt.xlabel('UV Distance (wavelengths)')
    #    plt.show()

    sub_vis_list = numpy.array_split(vis_list, size)
    sub_vis_list = comm.scatter(sub_vis_list, root=0)

    #NOTE: sub_vis_list is scattered (future_vis_list in arlexecute)

    # Find the best imaging parameters but don't bring the vis_list back here
    print("****** Finding wide field parameters ******")

    sub_advice = [
        advise_wide_field(v,
                          guard_band_image=6.0,
                          delA=0.1,
                          facets=facets,
                          wprojection_planes=wprojection_planes,
                          oversampling_synthesised_beam=4.0)
        for v in sub_vis_list
    ]
    # MONTSE: This does not make sense cause they all compute advice but only
    # the last one is used. Confirm that this is what the dask version does!!!
    advice_list = comm.gather(sub_advice, root=0)
    if rank == 0:
        advice = numpy.concatenate(advice_list)[-1]
    else:
        advice = None
    advice = comm.bcast(advice, root=0)

    # Deconvolution via sub-images requires 2^n
    npixel = advice['npixels2']
    results['npixel'] = npixel
    cellsize = advice['cellsize']
    results['cellsize'] = cellsize
    lprint("Image will have %d by %d pixels, cellsize = %.6f rad" %
           (npixel, npixel, cellsize))

    # NOTE: frequency and channel_bandwidth are replicated

    sub_frequency = numpy.array_split(frequency, size)
    sub_channel_bandwidth = numpy.array_split(channel_bandwidth, size)
    # Create an empty model image
    tmp_model_list = [
        create_image(npixel=npixel,
                     cellsize=cellsize,
                     frequency=[sub_frequency[rank][f]],
                     channel_bandwidth=[sub_channel_bandwidth[rank][f]],
                     phasecentre=phasecentre,
                     polarisation_frame=PolarisationFrame("stokesI"))
        for f, freq in enumerate(sub_frequency[rank])
    ]

    model_list = comm.gather(tmp_model_list, root=0)
    if rank == 0:
        model_list = numpy.concatenate(model_list).tolist()
    else:
        model_list = list()
    print(type(model_list))
    assert isinstance(model_list, list), model_list

    # NOTE: tmp_model_list is the scattered model_list
    # NOTE: template_model and gcfcf are replicated

    lprint("****** Setting up imaging parameters ******")
    # Now set up the imaging parameters
    template_model = create_image(
        npixel=npixel,
        cellsize=cellsize,
        frequency=[frequency[centre]],
        phasecentre=phasecentre,
        channel_bandwidth=[channel_bandwidth[centre]],
        polarisation_frame=PolarisationFrame("stokesI"))
    gcfcf = [create_pswf_convolutionfunction(template_model)]

    if context == 'timeslice':
        vis_slices = ntimes
        lprint("Using timeslice with %d slices" % vis_slices)
    elif context == '2d':
        vis_slices = 1
    elif context == "wprojection":
        wstep = advice['wstep']
        nw = advice['wprojection_planes']
        vis_slices = 1
        support = advice['nwpixels']
        results['wprojection_planes'] = nw

        lprint("****** Starting W projection kernel creation ******")
        lprint("Using wprojection with %d planes with wstep %.1f wavelengths" %
               (nw, wstep))
        lprint("Support of wprojection = %d pixels" % support)
        gcfcf = [
            create_awterm_convolutionfunction(template_model,
                                              nw=nw,
                                              wstep=wstep,
                                              oversampling=4,
                                              support=support,
                                              use_aaf=True)
        ]
        lprint("Size of W projection gcf, cf = %.2E bytes" % get_size(gcfcf))
    else:
        context = 'wstack'
        vis_slices = advice['vis_slices']
        lprint("Using wstack with %d slices" % vis_slices)

    results['vis_slices'] = vis_slices

    # Make a skymodel from gleam, with bright sources as components and weak sources in an image
    lprint("****** Starting GLEAM skymodel creation ******")
    future_skymodel_list = [
        create_low_test_skymodel_from_gleam(
            npixel=npixel,
            cellsize=cellsize,
            frequency=[sub_frequency[rank][f]],
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"),
            flux_limit=flux_limit,
            flux_threshold=dft_threshold,
            flux_max=5.0) for f, freq in enumerate(sub_frequency[rank])
    ]

    # We use predict_skymodel so that we can use skycomponents as well as images
    lprint("****** Starting GLEAM skymodel prediction ******")
    #NOTE: future_skymodel_list is a subset of skymodel_list which has not been
    #gathered
    sub_predicted_vis_list = [
        predict_skymodel_list_serial_workflow(sub_vis_list[f],
                                              [future_skymodel_list[f]],
                                              context=context,
                                              vis_slices=vis_slices,
                                              facets=facets,
                                              gcfcf=gcfcf)[0]
        for f, freq in enumerate(sub_frequency[rank])
    ]

    predicted_vis_list = comm.gather(sub_predicted_vis_list, root=0)
    if rank == 0:
        predicted_vis_list = numpy.concatenate(predicted_vis_list)
    else:
        predicted_vis_list = list()

    #NOTE: sub_predicted_vis_list is predicted_vis_list scattered
    # Corrupt the visibility for the GLEAM model
    lprint("****** Visibility corruption ******")
    tmp_corrupted_vis_list = corrupt_list_serial_workflow(
        sub_predicted_vis_list, phase_error=1.0, seed=seed)
    lprint("****** Weighting and tapering ******")
    tmp_corrupted_vis_list = weight_list_serial_workflow(
        tmp_corrupted_vis_list, tmp_model_list)
    tmp_corrupted_vis_list = taper_list_serial_workflow(
        tmp_corrupted_vis_list, 0.003 * 750.0 / rmax)
    corrupted_vis_list = comm.gather(tmp_corrupted_vis_list, root=0)
    if rank == 0:
        corrupted_vis_list = numpy.concatenate(corrupted_vis_list)
    else:
        corrupted_vis_list = list()

    #future_corrupted_vis_list = arlexecute.scatter(corrupted_vis_list)

    # At this point the only futures are of scatter'ed data so no repeated calculations should be
    # incurred.
    lprint("****** Starting dirty image calculation ******")
    start = time.time()
    dirty_list = invert_list_mpi_workflow(corrupted_vis_list,
                                          model_list,
                                          vis_slices=vis_slices,
                                          context=context,
                                          facets=facets,
                                          use_serial_invert=use_serial_imaging,
                                          gcfcf=gcfcf)

    end = time.time()
    results['size invert graph'] = get_size(dirty_list)
    lprint('Size of dirty graph is %.3E bytes' %
           (results['size invert graph']))
    results['time invert graph'] = 0.0
    results['time invert'] = end - start
    lprint("Dirty image invert took %.3f seconds" % (end - start))
    if rank == 0:
        dirty, sumwt = dirty_list[centre]
        lprint("Maximum in dirty image is %f, sumwt is %s" %
               (numpy.max(numpy.abs(dirty.data)), str(sumwt)))
        qa = qa_image(dirty)
        results['dirty_max'] = qa.data['max']
        results['dirty_min'] = qa.data['min']
    if write_fits and rank == 0:
        export_image_to_fits(
            dirty, "pipelines_arlexecute_timings-%s-dirty.fits" % context)

    lprint("****** Starting prediction ******")
    start = time.time()
    result = predict_list_mpi_workflow(corrupted_vis_list,
                                       model_list,
                                       vis_slices=vis_slices,
                                       context=context,
                                       facets=facets,
                                       use_serial_predict=use_serial_imaging,
                                       gcfcf=gcfcf)
    # arlexecute.client.cancel(tmp_vis_list)
    end = time.time()
    results['time predict'] = end - start
    lprint("Predict took %.3f seconds" % (end - start))

    # Create the ICAL pipeline to run major cycles, starting selfcal at cycle 1. A global solution across all
    # frequencies (i.e. Visibilities) is performed.

    print("Using subimage clean")
    deconvolve_facets = 8
    deconvolve_overlap = 16
    deconvolve_taper = 'tukey'

    lprint("****** Starting ICAL graph creation ******")

    controls = create_calibration_controls()

    controls['T']['first_selfcal'] = 1
    controls['T']['timescale'] = 'auto'

    start = time.time()
    ical_list = ical_list_mpi_workflow(corrupted_vis_list,
                                       model_imagelist=model_list,
                                       context=context,
                                       vis_slices=vis_slices,
                                       scales=[0, 3, 10],
                                       algorithm='mmclean',
                                       nmoment=3,
                                       niter=1000,
                                       fractional_threshold=0.1,
                                       threshold=0.01,
                                       nmajor=nmajor,
                                       gain=0.25,
                                       psf_support=64,
                                       deconvolve_facets=deconvolve_facets,
                                       deconvolve_overlap=deconvolve_overlap,
                                       deconvolve_taper=deconvolve_taper,
                                       timeslice='auto',
                                       global_solution=True,
                                       do_selfcal=True,
                                       calibration_context='T',
                                       controls=controls,
                                       use_serial_predict=use_serial_imaging,
                                       use_serial_invert=use_serial_imaging,
                                       use_serial_clean=use_serial_clean,
                                       gcfcf=gcfcf)

    end = time.time()
    results['size ICAL graph'] = get_size(ical_list)
    lprint('Size of ICAL graph is %.3E bytes' % results['size ICAL graph'])
    results['time ICAL graph'] = 0.0
    # Execute the graph
    lprint("****** Executing ICAL graph ******")
    deconvolved, residual, restored, gaintables = ical_list

    results['time ICAL'] = end - start
    lprint("ICAL graph execution took %.3f seconds" % (end - start))
    if rank == 0:
        qa = qa_image(deconvolved[centre])
        results['deconvolved_max'] = qa.data['max']
        results['deconvolved_min'] = qa.data['min']
        deconvolved_cube = image_gather_channels(deconvolved)
        if write_fits:
            export_image_to_fits(
                deconvolved_cube,
                "pipelines_arlexecute_timings-%s-ical_deconvolved.fits" %
                context)

        qa = qa_image(residual[centre][0])
        results['residual_max'] = qa.data['max']
        results['residual_min'] = qa.data['min']
        residual_cube = remove_sumwt(residual)
        residual_cube = image_gather_channels(residual_cube)
        if write_fits:
            export_image_to_fits(
                residual_cube,
                "pipelines_arlexecute_timings-%s-ical_residual.fits" % context)

        qa = qa_image(restored[centre])
        results['restored_max'] = qa.data['max']
        results['restored_min'] = qa.data['min']
        restored_cube = image_gather_channels(restored)
        if write_fits:
            export_image_to_fits(
                restored_cube,
                "pipelines_arlexecute_timings-%s-ical_restored.fits" % context)
    #

    end_all = time.time()
    results['time overall'] = end_all - start_all

    lprint("At end, results are:")
    results = sort_dict(results)
    lprint(results)

    return results