Esempio n. 1
0
def getArrayFromNT(table, nrows=0):
    bd = BlockDescriptor_Float64()
    if nrows == 0:
        nrows = table.getNumberOfRows()
    table.getBlockOfRows(0, nrows, readOnly, bd)
    npa = bd.getArray()
    table.releaseBlockOfRows(bd)
    return npa

if __name__ == "__main__":
    trainDatasetFileNames = getDatasetFileNames('news_train_dense_dist_data_*.csv')
    comm = MPI.COMM_WORLD
    comm_size = comm.Get_size()
    rankId = comm.Get_rank()
    print("I am a worker with rank %d on %s." % (rankId, MPI.Get_processor_name()))
    start = MPI.Wtime()
    trainModel()
    if rankId == MPI_ROOT:
        end = MPI.Wtime()
        testModel()
        testGroundTruth = FileDataSource(testGroundTruthFileName,
                                         DataSourceIface.doAllocateNumericTable,
                                         DataSourceIface.doDictionaryFromContext)
        testGroundTruth.loadDataBlock()
        a = getArrayFromNT(predictionResult.get(classifier.prediction.prediction))
        b = getArrayFromNT(testGroundTruth.getNumericTable())
        acc = metrics.accuracy_score(a, b, normalize=True)
        print('Accuracy: {:.4f}'.format(acc))
        print('Computational time: {:.2f}'.format(end - start))
Esempio n. 2
0
def getprocessorinfo():
    from mpi4py import MPI
    rank = MPI.COMM_WORLD.Get_rank()
    name = MPI.Get_processor_name()
    return (rank, name)
Esempio n. 3
0
#writing ensight Gold binary (comaptible with vtk 8)
#parallelization borowed here https://gist.github.com/fspaolo/51eaf5a20d6d418bd4d0
import numpy as np  #needs install
import os
import pandas  #needs install

from mpi4py import MPI  #needs install
from queue import Queue

WORKTAG = 1
DIETAG = 0

comm = MPI.COMM_WORLD
my_rank = comm.Get_rank()
my_name = MPI.Get_processor_name()

# In[9]:
#-------------------------------------------------
#
# Parse arguments
#
#-------------------------------------------------

inputfolder = None
project_name = None
outputfolder = None

try:
    if my_rank == 0:
        import argparse
Esempio n. 4
0
def v1_kcat_mwc_validate():
    name = MPI.Get_processor_name()
    rank = MPI.COMM_WORLD.Get_rank()
    size = MPI.COMM_WORLD.Get_size()

    if rank == 0:
        # create ident object first
        v1_ident = ModelIdent(
            ident_fun=kotte_model.flux_1_kcat_ident,
            arranged_data_file_name=os.path.join(
                os.getcwd(), 'exp/exp_v1_2_experiments_mwc'),
            ident_data_file_name=os.path.join(os.getcwd(),
                                              'ident/ident_v1_kcat_mwc'),
            **{
                'original_exp_file':
                os.path.join(os.getcwd(), 'exp/experiments_mwc'),
                'flux_id':
                1,
                'flux_choice':
                2,
                'values_figure':
                os.path.join(os.getcwd(),
                             'results/v1_kcat_mwc_parameter_values.eps'),
                'ident_figure':
                os.path.join(os.getcwd(), 'results/v1_kcat_mwc_ident.eps'),
                'exp_figure':
                os.path.join(os.getcwd(), 'results/v1_kcat_mwc_exp.eps'),
                'figure_format':
                'eps',
                'ident_index_label': ['sample_name', 'data_set_id']
            })

        # retrieve identifiability data and process it for validation
        v1_ident.validation_info()

        user_ode_opts = {
            'iter': 'Newton',
            'discr': 'Adams',
            'atol': 1e-10,
            'rtol': 1e-10,
            'time_points': 200,
            'display_progress': True,
            'verbosity': 30
        }
        # initial ss to begin all simulations from
        y0 = np.array([5, 1, 1])
        # get and set true parameter values, if available separately
        default_parameters = true_parameter_values()

        v1_valid_obj = ValidateSim(
            kotte_model.kotte_ode, kotte_model.kotte_flux, **{
                'kinetics':
                1,
                'ode_opts':
                user_ode_opts,
                't_final':
                200,
                'wt_y0':
                y0,
                'i_parameter':
                default_parameters,
                'sample_size':
                1,
                'noise_std':
                0.05,
                'validate_index_label':
                ['estimate_id', 'sample_name', 'data_set_id', 'experiment_id'],
                'validate_file_name':
                os.path.join(os.getcwd(), 'validate/v1_kcat_mwc_validate'),
                'original_exp_file':
                v1_ident.original_exp_file,
                'c_validate_file':
                os.path.join(os.getcwd(),
                             'results/v1_kcat_mwc_c_validate.eps'),
                'f_validate_file':
                os.path.join(os.getcwd(),
                             'results/v1_kcat_mwc_f_validate.eps'),
                'c_exp_file':
                os.path.join(os.getcwd(), 'results/v1_kcat_mwc_c_exp.eps'),
                'v_exp_file':
                os.path.join(os.getcwd(), 'results/v1_kcat_mwc_f_exp.eps'),
                'format':
                v1_ident.figure_format
            })

        parameter_estimates, estimate_info = v1_valid_obj.create_parameter_list(
            v1_ident.select_values)

        job = ParallelValidate(slaves=range(1, size))

        validate_results = job.run_all(task='initial_sim',
                                       **{
                                           'parameters': parameter_estimates,
                                           'estimate_info': estimate_info,
                                           'sim_obj': v1_valid_obj
                                       })
        job.terminate_slaves()

        # process validation data for plots
        validate_processing(v1_valid_obj, validate_results)

    else:

        print('I am %s Slave with rank %s of %s' %
              (name, str(rank), str(size)))
        ValidateSlave().run()

    return None
Esempio n. 5
0
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
node_name = MPI.Get_processor_name()  # get the name of the node
print('Hello world from process %d at %s.' % (rank, size))
        process_file(f)
    #send unfinished buffers
    for id_dest in range(cnt_reducers):
        for pos_buf in range(pos_bufers[id_dest], size_buffer):
            buffers[id_dest][pos_buf][0] = -1
        comm.Send(buffers[id_dest], dest=id_dest + cnt_mappers, tag=1)
    print("m{} finished! sppend {:.2f} s on waiting Send".format(
        id_worker, timings["wait_send"]))
    #print (lst_files)
else:  #this is reducer
    timings["collecting"] -= timer()
    rstart, rend = get_interval(cnt_words, cnt_reducers, id_reducer)
    m = ArrayOfTrees(rend - rstart)
    print(
        "I'm reducer {} of {} running on {}, my ownership range is from {} to {}"
        .format(id_reducer, cnt_reducers, MPI.Get_processor_name(), rstart,
                rend))
    buffer = np.empty((size_buffer, 2), dtype=np.int64)
    cnt_mappers_finished = 0
    has_work = True
    while has_work:
        #print("r{}: waiting rcv".format(id_reducer))
        sys.stdout.flush()
        comm.Recv(buffer, source=MPI.ANY_SOURCE, tag=1)
        #print ("r{} recvd {}".format(id_reducer,buffer.shape))
        for i in range(size_buffer):
            if buffer[i, 0] >= 0:
                m.accumulate(int(buffer[i][0] - rstart),
                             int(buffer[i][1]))  #todo mapping for aot
            else:
                cnt_mappers_finished += 1
Esempio n. 7
0
def classify_subvolumes():
    """ 
    Assigns each pixels to the class with the highest probability value determined by Ilastik classifier.
    
    Ilastik returns probability map in the "exported_data" dataset with the dimensions of x,y,z 
    (pixel location) by N values and N is the number of classes (labels) defined in the Ilastik 
    trained data file. This script adds N datasets to each of the input hdf5 files. Dimension of
    each dataset is x,y,z (pixel location) and value of zero or one. The pixel location in the added
    datasets with the highest probability value is one, zero otherwise.
    
    Input: Ilastik sub-volumes pixel classified hdf5 files - file location is specified in seg_user_param.py.
    
    Output: Datasets added to the sub-volume input files - one dataset per each class/label in trained data.
    """

    start_time = time.time()
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = MPI.COMM_WORLD.Get_size()
    name = MPI.Get_processor_name()

    ilastik_classes = get_ilastik_labels()
    if rank == 0:
        print("*** Time is %d Entered classify_subvolumes() ****" %
              time.time())
        print("Ilastik classes are ", ilastik_classes)

    # Get list of pixel classified sub-volume files with probability maps . Assumes file extension is .h5
    input_files = sorted(glob(hdf_subvol_files_location +
                              '/*Probabilities.h5'))
    if not input_files:
        print("*** Did not find any file ending with .h5 extension  ***",
              hdf_subvol_files_location)
        return

    if rank == 0:
        print("Number of HDF5 files is %d, and Number of processes is %d" %
              ((len(input_files)), size))
        print("Sub-Volume file location is %s" % hdf_subvol_files_location)
    iterations = int(len(input_files) / size) + (len(input_files) % size > 0)
    for idx in range(iterations):
        if (rank + (size * idx)) >= len(input_files):
            print(
                "\nBREAKING out, my rank is %d, number of files is %d, size is %d and idx is %d"
                % (rank, len(input_files), size, idx))
            break

        infile = h5py.File(input_files[rank + (size * idx)], 'r+')
        exds = infile[ilastik_ds_name]
        start_read = time.time()
        export_indata = exds[...]
        export_outdata = np.zeros(export_indata.shape, dtype='uint8')
        print("export_indata data shape is", export_indata.shape)
        end_read = time.time()
        print("Read time for all rows of Ilastik Prob map is %d Sec" %
              (end_read - start_read))
        print_cycle = 100
        start_loop_time = time.time()
        for row in range(export_outdata.shape[0]):
            for colmn in range(export_outdata.shape[1]):
                outdata = np.zeros(
                    (export_outdata.shape[2], export_outdata.shape[3]),
                    'uint8')
                if row == 0:
                    if colmn == 0:
                        print("outdata shape is", outdata.shape)
                outdata[np.arange(len(outdata)),
                        np.argmax(export_indata[row,
                                                colmn, :, :], axis=-1)] = 1
                if np.count_nonzero(outdata) != export_outdata.shape[2]:
                    print(
                        "Something is very wrong - check row %d and colmn %d" %
                        (row, colmn))
                export_outdata[row, colmn, :, :] = outdata.copy()
            if row % print_cycle == 0:
                print("time to classify %d rows is %d Sec" %
                      (print_cycle, (time.time() - start_loop_time)))
                start_loop_time = time.time()
        # Segment classes and saved them into datasets
        start_ds_time = time.time()
        no_of_classes = export_outdata.shape[3]
        export_outdata = np.transpose(export_outdata, (3, 0, 1, 2))
        for idx in range(no_of_classes):
            time_per_ds = time.time()
            if infile.get(ilastik_classes[idx], getclass=True):
                print("*** Deleting subvolume object map %s ***" %
                      (ilastik_classes[idx]))
                infile.__delitem__(ilastik_classes[idx])
            outdataset = infile.create_dataset(
                ilastik_classes[idx],
                (export_outdata.shape[1], export_outdata.shape[2],
                 export_outdata.shape[3]), export_outdata.dtype)
            outdataset[...] = export_outdata[idx, ...]
            print("Time to save %s dataset is %d Sec" %
                  (ilastik_classes[idx], (time.time() - time_per_ds)))
        # Print time to save datasets
        print("Time to save all object maps is %d Sec" %
              (time.time() - start_ds_time))
        infile.close()
    print("Exec time for this function was %d Sec and rank is %d" %
          ((time.time() - start_time), rank))
 def set_MPI(self, comm):
     self.rank = comm.Get_rank()
     self.size = comm.Get_size()
     self.name = MPI.Get_processor_name()
     return self
Esempio n. 9
0
def main():
    comm = MPI.COMM_WORLD

    comm.Barrier()
    wt = MPI.Wtime()
    rank = comm.Get_rank()
    size = comm.Get_size()
    processorName = MPI.Get_processor_name()

    BLOCKS = int(sqrt(size))

    for k in range(BLOCKS + 1):
        i = int(rank / BLOCKS)
        j = int(rank % BLOCKS)
        if k == 0:
            if rank == 0:
                A = read_matrix_from_file('input/matrixA')

                #B = read_matrix_from_file('input/matrixB')

                SUB_ELEM = int(len(A) / BLOCKS)

                ma = divide_into_blocks(A, BLOCKS)
                #mb = divide_into_blocks(B, BLOCKS)
                ma = initial_shift_left(ma)
                #mb = initial_shift_up(mb)

                for n in range(BLOCKS):
                    for m in range(BLOCKS):
                        if n == 0 and m == 0:
                            continue
                        comm.send(ma[n][m], dest=(n * BLOCKS + m), tag=1)
                        #comm.send(mb[n][m], dest=(n * BLOCKS + m), tag=2)

                dataA = ma[0][0]
                #dataB = mb[0][0]
                dataB = comm.recv(source=1, tag=2)
                C = [[0 for i in range(SUB_ELEM)] for j in range(SUB_ELEM)]
            elif rank == 1:
                B = read_matrix_from_file('input/matrixB')
                SUB_ELEM = int(len(B) / BLOCKS)
                mb = divide_into_blocks(B, BLOCKS)
                mb = initial_shift_up(mb)
                for n in range(BLOCKS):
                    for m in range(BLOCKS):
                        if n == 0 and m == 1:
                            continue
                        comm.send(mb[n][m], dest=(n * BLOCKS + m), tag=2)
                dataB = mb[0][1]
                dataA = comm.recv(source=0, tag=1)
                C = [[0 for i in range(SUB_ELEM)] for j in range(SUB_ELEM)]
            else:
                dataA = comm.recv(source=0, tag=1)
                dataB = comm.recv(source=1, tag=2)
                SUB_ELEM = int(len(dataA))
                C = [[0 for i in range(SUB_ELEM)] for j in range(SUB_ELEM)]

        else:
            # izracunaj
            C = multiple_matrixes(dataA, dataB, C, 0, 0)

            comm.send(dataA,
                      dest=(i * BLOCKS + (j + BLOCKS - 1) % BLOCKS),
                      tag=1)
            comm.send(dataB,
                      dest=(((i + BLOCKS - 1) % BLOCKS) * BLOCKS + j),
                      tag=2)

            dataA = comm.recv(source=(i * BLOCKS + (j + 1) % BLOCKS), tag=1)
            dataB = comm.recv(source=(((i + 1) % BLOCKS) * BLOCKS + j), tag=2)

    if rank == 0:
        blocks = [[] for i in range(size)]
        blocks[0] = C
        for i in range(1, size):
            blocks[i] = comm.recv(source=i)

        res = [[0 for i in range(len(A))] for j in range(len(A[0]))]
        res = blocks_to_matrix(blocks, res)
        write_matrix_to_file("output/parallelOutput", res)
    else:
        comm.send(C, dest=0)

    comm.Barrier()

    if rank == 0:
        wt = MPI.Wtime() - wt
        print("Vreme: ", wt)
Esempio n. 10
0
def run_mpi_sim(args, inputfile, usernamespace, optparams=None):
    """
    Run mixed mode MPI/OpenMP simulation - MPI task farm for models with
    each model parallelised using either OpenMP (CPU) or CUDA (GPU)

    Args:
        args (dict): Namespace with command line arguments
        inputfile (object): File object for the input file.
        usernamespace (dict): Namespace that can be accessed by user in any
                Python code blocks in input file.
        optparams (dict): Optional argument. For Taguchi optimisation it
                provides the parameters to optimise and their values.
    """

    from mpi4py import MPI

    status = MPI.Status()
    hostname = MPI.Get_processor_name()

    # Set range for number of models to run
    modelstart = args.restart if args.restart else 1
    modelend = modelstart + args.n
    numbermodelruns = args.n

    # Command line flag used to indicate a spawned worker instance
    workerflag = '--mpi-worker'
    numworkers = args.mpi - 1

    ##################
    # Master process #
    ##################
    if workerflag not in sys.argv:
        # N.B Spawned worker flag (--mpi-worker) applied to sys.argv when MPI.Spawn is called

        # Get MPI communicator object either through argument or just get comm_world
        if hasattr(args, 'mpicomm'):
            comm = args.mpicomm
        else:
            comm = MPI.COMM_WORLD
        rank = comm.Get_rank()  # rank of this process
        tsimstart = perf_counter()
        print('MPI master ({}, rank {}) on {} using {} workers\n'.format(
            comm.name, rank, hostname, numworkers))

        # Assemble a sys.argv replacement to pass to spawned worker
        # N.B This is required as sys.argv not available when gprMax is called via api()
        # Ignore mpicomm object if it exists as only strings can be passed via spawn
        myargv = []
        for key, value in vars(args).items():
            if value:
                # Input file name always comes first
                if 'inputfile' in key:
                    myargv.append(value)
                elif 'gpu' in key:
                    myargv.append('-' + key)
                    # If specific GPU device ID is given then add it
                    if not isinstance(value, list):
                        myargv.append(str(value.deviceID))
                elif 'mpicomm' in key:
                    pass
                elif '_' in key:
                    key = key.replace('_', '-')
                    myargv.append('--' + key)
                else:
                    myargv.append('-' + key)
                    if value is not True:
                        myargv.append(str(value))

        # Create a list of work
        worklist = []
        for model in range(modelstart, modelend):
            workobj = dict()
            workobj['currentmodelrun'] = model
            if optparams:
                workobj['optparams'] = optparams
            worklist.append(workobj)
        # Add stop sentinels
        worklist += ([StopIteration] * numworkers)

        # Spawn workers
        newcomm = comm.Spawn(sys.executable,
                             args=['-m', 'gprMax'] + myargv + [workerflag],
                             maxprocs=numworkers)

        # Reply to whoever asks until done
        for work in worklist:
            newcomm.recv(source=MPI.ANY_SOURCE, status=status)
            newcomm.send(obj=work, dest=status.Get_source())

        # Shutdown communicators
        newcomm.Disconnect()

        tsimend = perf_counter()
        simcompletestr = '\n=== Simulation completed in [HH:MM:SS]: {}'.format(
            datetime.timedelta(seconds=tsimend - tsimstart))
        print('{} {}\n'.format(
            simcompletestr,
            '=' * (get_terminal_width() - 1 - len(simcompletestr))))

    ##################
    # Worker process #
    ##################
    elif workerflag in sys.argv:
        print('Worker')
        # Connect to parent to get communicator
        try:
            comm = MPI.Comm.Get_parent()
            rank = comm.Get_rank()
        except ValueError:
            raise ValueError('MPI worker could not connect to parent')

        # Ask for work until stop sentinel
        for work in iter(lambda: comm.sendrecv(0, dest=0), StopIteration):
            currentmodelrun = work['currentmodelrun']

            # Get info and setup device ID for GPU(s)
            gpuinfo = ''
            if args.gpu is not None:
                # Set device ID for multiple GPUs
                if isinstance(args.gpu, list):
                    deviceID = (rank - 1) % len(args.gpu)
                    args.gpu = next(gpu for gpu in args.gpu
                                    if gpu.deviceID == deviceID)
                gpuinfo = ' using {} - {}, {} RAM '.format(
                    args.gpu.deviceID, args.gpu.name,
                    human_size(args.gpu.totalmem,
                               a_kilobyte_is_1024_bytes=True))

            # If Taguchi optimistaion, add specific value for each parameter to
            # optimise for each experiment to user accessible namespace
            if 'optparams' in work:
                tmp = {}
                tmp.update((key, value[currentmodelrun - 1])
                           for key, value in work['optparams'].items())
                modelusernamespace = usernamespace.copy()
                modelusernamespace.update({'optparams': tmp})
            else:
                modelusernamespace = usernamespace

            # Run the model
            print('MPI worker (rank {}) starting model {}/{}{} on {}\n'.format(
                rank, currentmodelrun, numbermodelruns, gpuinfo, hostname))
            run_model(args, currentmodelrun, modelend - 1, numbermodelruns,
                      inputfile, modelusernamespace)

        # Shutdown
        comm.Disconnect()
Esempio n. 11
0
def run_mpi_alt_sim(args, inputfile, usernamespace, optparams=None):
    """
    Alternate MPI implementation that avoids using the spawn mechanism.
    This implementation is designed to be used as
    e.g. 'mpirun -n 5 python -m gprMax user_models/mymodel.in -n 10 -mpialt'

    Run mixed mode MPI/OpenMP simulation - MPI task farm for models with
    each model parallelised using either OpenMP (CPU) or CUDA (GPU)

    Args:
        args (dict): Namespace with command line arguments
        inputfile (object): File object for the input file.
        usernamespace (dict): Namespace that can be accessed by user in any
                Python code blocks in input file.
        optparams (dict): Optional argument. For Taguchi optimisation it
                provides the parameters to optimise and their values.
    """

    from mpi4py import MPI

    # Define MPI message tags
    tags = Enum('tags', {'READY': 0, 'DONE': 1, 'EXIT': 2, 'START': 3})

    # Initializations and preliminaries
    comm = MPI.COMM_WORLD
    size = comm.Get_size()  # total number of processes
    rank = comm.Get_rank()  # rank of this process
    status = MPI.Status()  # get MPI status object
    hostname = MPI.Get_processor_name()  # get name of processor/host

    # Set range for number of models to run
    modelstart = args.restart if args.restart else 1
    modelend = modelstart + args.n
    numbermodelruns = args.n
    currentmodelrun = modelstart  # can use -task argument to start numbering from something other than 1
    numworkers = size - 1

    ##################
    # Master process #
    ##################
    if rank == 0:
        tsimstart = perf_counter()
        print('MPI master ({}, rank {}) on {} using {} workers\n'.format(
            comm.name, rank, hostname, numworkers))

        closedworkers = 0
        while closedworkers < numworkers:
            comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
            source = status.Get_source()
            tag = status.Get_tag()

            # Worker is ready, so send it a task
            if tag == tags.READY.value:
                if currentmodelrun < modelend:
                    comm.send(currentmodelrun,
                              dest=source,
                              tag=tags.START.value)
                    currentmodelrun += 1
                else:
                    comm.send(None, dest=source, tag=tags.EXIT.value)

            # Worker has completed a task
            elif tag == tags.DONE.value:
                pass

            # Worker has completed all tasks
            elif tag == tags.EXIT.value:
                closedworkers += 1

        tsimend = perf_counter()
        simcompletestr = '\n=== Simulation completed in [HH:MM:SS]: {}'.format(
            datetime.timedelta(seconds=tsimend - tsimstart))
        print('{} {}\n'.format(
            simcompletestr,
            '=' * (get_terminal_width() - 1 - len(simcompletestr))))

    ##################
    # Worker process #
    ##################
    else:
        while True:
            comm.send(None, dest=0, tag=tags.READY.value)
            # Receive a model number to run from the master
            currentmodelrun = comm.recv(source=0,
                                        tag=MPI.ANY_TAG,
                                        status=status)
            tag = status.Get_tag()

            # Run a model
            if tag == tags.START.value:

                # Get info and setup device ID for GPU(s)
                gpuinfo = ''
                if args.gpu is not None:
                    # Set device ID for multiple GPUs
                    if isinstance(args.gpu, list):
                        deviceID = (rank - 1) % len(args.gpu)
                        args.gpu = next(gpu for gpu in args.gpu
                                        if gpu.deviceID == deviceID)
                    gpuinfo = ' using {} - {}, {}'.format(
                        args.gpu.deviceID, args.gpu.name,
                        human_size(args.gpu.totalmem,
                                   a_kilobyte_is_1024_bytes=True))

                # If Taguchi optimistaion, add specific value for each parameter
                # to optimise for each experiment to user accessible namespace
                if optparams:
                    tmp = {}
                    tmp.update((key, value[currentmodelrun - 1])
                               for key, value in optparams.items())
                    modelusernamespace = usernamespace.copy()
                    modelusernamespace.update({'optparams': tmp})
                else:
                    modelusernamespace = usernamespace

                # Run the model
                print('MPI worker (rank {}) starting model {}/{}{} on {}\n'.
                      format(rank, currentmodelrun, numbermodelruns, gpuinfo,
                             hostname))
                run_model(args, currentmodelrun, modelend - 1, numbermodelruns,
                          inputfile, modelusernamespace)
                comm.send(None, dest=0, tag=tags.DONE.value)

            # Break out of loop when work receives exit message
            elif tag == tags.EXIT.value:
                break

        comm.send(None, dest=0, tag=tags.EXIT.value)
Esempio n. 12
0
from mpi4py import MPI
comm = MPI.COMM_WORLD
print('Hello from {}. {} of {}'.format(MPI.Get_processor_name(), comm.rank,
                                       comm.size))
Esempio n. 13
0
    def go(self):
        if rank == 0:
            self.initialize_controller()
            self.complete_snp_task_count = 0
            with open(self.output_vcf_fp, 'w') as self.output_file, \
                 open(self.output_cv_scores_fp, 'w') as self.output_cv_scores_file:
                # this process is the controller
                # while there are still running workers wait for a work request
                num_workers = size - 1
                a_task_gen = self.get_task()
                # need a fake task that is not None to get started
                a_task = object()
                while num_workers > 0:
                    msg = comm.recv(source=MPI.ANY_SOURCE,
                                    tag=MPI.ANY_TAG,
                                    status=status)
                    source = status.Get_source()
                    tag = status.Get_tag()
                    print(
                        '[controller] recv message from worker {} with tag {}'.
                        format(source, tag))
                    if tag == READY_ and a_task is None:
                        # if a_task is None there are no more SNPs to test
                        # and we should not call next(a_task_gen) again
                        print('[controller] sending exit message to worker {}'.
                              format(source))
                        comm.send(None, dest=source, tag=EXIT_)
                    elif tag == READY_ and a_task is not None:
                        a_task = next(a_task_gen)
                        if a_task is None:
                            print(
                                '[controller] sending exit message to worker {}'
                                .format(source))
                            comm.send(None, dest=source, tag=EXIT_)
                        else:
                            print(
                                '[controller] sending a task to worker {} with SNP {} {}'
                                .format(source, a_task.snp_with_rsq_df.GENE[0],
                                        a_task.snp_with_rsq_df.ID[0]))
                            comm.send(a_task, dest=source, tag=START_)
                    elif tag == DONE_:
                        # save the results
                        self.complete_snp_task_count += 1
                        self.task_complete(msg)
                        print(
                            '[controller] received a processed task from worker {}'
                            .format(source))
                    elif tag == EXIT_:
                        print(
                            '[controller] received exit message from worker {}'
                            .format(source))
                        num_workers -= 1
                    elif tag == EXCEPTION_:
                        print(
                            '[controller] received exception message from worker {} with SNP {} {}'
                            .format(source, msg.snp_with_rsq_df.GENE[0],
                                    msg.snp_with_rsq_df.ID[0]))
                        self.task_failed(msg)
                        num_workers -= 1
                    else:
                        print(
                            '[controller] unrecognized message from source {} with tag {}:\n{}'
                            .format(source, tag, msg))
                print('[controller] all workers have exited')
        else:
            # this process is a worker
            self.initialize_worker()
            worker_t0 = time.time()
            task_count = 0
            name = MPI.Get_processor_name()
            print("[worker {}] running on {}".format(rank, name))
            while True:
                print('[worker {}] sending request for work'.format(rank))
                comm.send(None, dest=0, tag=READY_)
                print('[worker {}] waiting for work'.format(rank))
                t0 = time.time()
                my_task = comm.recv(source=0, tag=MPI.ANY_TAG, status=status)
                t1 = time.time()
                print('[worker {}] waited {:4.2f}s for work'.format(
                    rank, t1 - t0))
                tag = status.Get_tag()
                print('[worker {}] received message with tag {}'.format(
                    rank, tag))
                if tag == START_:
                    try:
                        t0 = time.time()
                        my_task.do()
                        t1 = time.time()
                        print('[worker {}] task time: {:5.2f}s'.format(
                            rank, t1 - t0))
                        comm.send(my_task, dest=0, tag=DONE_)
                        task_count += 1
                    except Exception as e:
                        print(
                            '[worker {}] quitting with exception'.format(rank))
                        traceback.print_exc()
                        comm.send(my_task, dest=0, tag=EXCEPTION_)
                        break
                elif tag == EXIT_:
                    print('[worker {}] received exit message'.format(rank))
                    comm.send(None, dest=0, tag=EXIT_)
                    break
                else:
                    # an unknown message was received - maybe from outer space?
                    print(
                        '[worker {}] received an unrecognized message with tag {} - exiting'
                        .format(rank, tag))
                    break

            worker_t1 = time.time()
            print('[worker {}] exiting after {} tasks in  {:6.2f}s'.format(
                rank, task_count, worker_t1 - worker_t0))
Esempio n. 14
0
def vessel_seg_post_proc():
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = MPI.COMM_WORLD.Get_size()
    name = MPI.Get_processor_name()
    start_time = int(time.time())
    # Get the list of all segmented sub-volume files.
    input_files = sorted(glob(outimage_file_location + '/*subvol*.h5'))
    if not input_files:
        print("*** Did not find any sub-volume segmented file in location %s ***" % outimage_file_location)
        return
    
    # Get the "Vessel" label index
    vessel_label_defined, vessel_label_idx = save_prob_map('Vessel')
    if vessel_label_defined == False:
        print("Vessel class is not labeled in the Ilastik training data file, no processing will take place")
        return
    # Shape/Dimension of the volume image is available in the last sub-volume file.
    volume_ds_shape = np.zeros((3,), dtype='uint64')
    # Last file has the dimensions for the volume.
    f = h5py.File(input_files[-1], 'r')
    volshape = f['orig_indices']
    volume_ds_shape[0] = volshape[1]
    volume_ds_shape[1] = volshape[3]
    volume_ds_shape[2] = volshape[5]
    f.close()
    # Get the list of segmented datasets
    # seg_ds_list = f.keys()
    labeld_obj = get_ilastik_labels()
    ds_name = labeld_obj[vessel_label_idx]
    # Create an hdf file to contain the post segmentation cell volume image.
    par, name = os.path.split(post_seg_volume_location)
    seg_volume_file = post_seg_volume_location + '/volume_vessel_' + name + '.h5'
    if rank == 0:
        print("Post segmentation directory is %s, number of file is %d and number of python processes is %d" % 
              (post_seg_volume_location, len(input_files), size))
        print("Volume shape is", volume_ds_shape)
    # Create directory for the post segmentation processing if it does not exist.
    if rank == 0:
        if not os.path.exists(post_seg_volume_location):
            os.mkdir(post_seg_volume_location)
            print("File directory for post segmentation did not exist, was created")
    comm.Barrier()
    
    # Need Parallel HDF for faster processing. However the below test lets processing to continue even if
    # Parallel HDF is not available.
    if size == 1:
        vol_img_file = h5py.File(seg_volume_file, 'w')
    else:
        vol_img_file = h5py.File(seg_volume_file, 'w', driver='mpio', comm=comm)
    
    if rank == 0:
        print("Dataset name to apply post processing is %s" % ds_name)
    vol_seg_dataset = vol_img_file.create_dataset(ds_name, volume_ds_shape, dtype='uint32',
                                                  chunks=(1, il_sub_vol_y, il_sub_vol_z))
    iterations = int(len(input_files) / size) + (len(input_files) % size > 0)
    for idx in range(iterations):
        if (rank + (size * idx)) >= len(input_files):
            print("\nBREAKING out, my rank is %d, number of files is %d, size is %d and idx is %d" % 
                  (rank, len(input_files), size, idx))
            break
        print("*** Working on file %s and rank is %d ***" % (input_files[rank + size * idx], rank))
        subvol_file = h5py.File(input_files[rank + size * idx], 'r')
        # Retrieve indices into the whole volume.
        orig_idx_ds = subvol_file['orig_indices']
        orig_idx = orig_idx_ds[...]
        
        # Retrieve overlap size to the right and left side of the sub-volume.
        right_overlapds = subvol_file['right_overlap']
        rightoverlap = right_overlapds[...]
        left_overlapds = subvol_file['left_overlap']
        leftoverlap = left_overlapds[...]
        
        myds = subvol_file[ds_name]
        subvoldata = myds[...]
        x_dim = subvoldata.shape[0]
        y_dim = subvoldata.shape[1]
        z_dim = subvoldata.shape[2]
        subvoldata = subvoldata > 0
        subvoldata = ndi.binary_fill_holes(subvoldata)
        subvoldata = morphology.erosion(subvoldata, morphology.ball(2))
        subvoldata = morphology.dilation(subvoldata, morphology.ball(2))
        subvoldata = ndi.binary_fill_holes(subvoldata)
        subvoldata = morphology.remove_small_objects(subvoldata, MINSZ_VESSEL, connectivity=2)
        subvoldata = morphology.label(subvoldata.astype('uint32'))
        
        vol_seg_dataset[orig_idx[0]:orig_idx[1], orig_idx[2]:orig_idx[3], orig_idx[4]:orig_idx[5]] = \
            subvoldata[leftoverlap[0] : x_dim - rightoverlap[0],
                       leftoverlap[1] : y_dim - rightoverlap[1],
                       leftoverlap[2] : z_dim - rightoverlap[2]]
        subvol_file.close()
    vol_img_file.close()
    print("Time to execute vessel_seg_post_proc() is %d seconds and rank is %d" % ((time.time() - start_time), rank))
Esempio n. 15
0
    def _setup_MPI_grid(self):
        """Split space up according to the number of MPI tasks. Set instance
        attributes for spatial extent and number of points in this MPI task,
        and create buffers and persistent communication requests for sending
        data to adjacent processes"""

        self.MPI_size = MPI.COMM_WORLD.Get_size()
        self.MPI_size_x, self.MPI_size_y = get_best_2D_segmentation(
            self.nx_global, self.ny_global, self.MPI_size)
        self.MPI_comm = MPI.COMM_WORLD.Create_cart(
            [self.MPI_size_x, self.MPI_size_y],
            periods=[self.periodic_x, self.periodic_y],
            reorder=True)
        self.MPI_rank = self.MPI_comm.Get_rank()
        self.MPI_x_coord, self.MPI_y_coord = self.MPI_comm.Get_coords(
            self.MPI_rank)
        if self.MPI_x_coord > 0 or self.periodic_x:
            self.MPI_rank_left = self.MPI_comm.Get_cart_rank(
                (self.MPI_x_coord - 1, self.MPI_y_coord))
        else:
            self.MPI_rank_left = MPI.PROC_NULL
        if self.MPI_x_coord < self.MPI_size_x - 1 or self.periodic_x:
            self.MPI_rank_right = self.MPI_comm.Get_cart_rank(
                (self.MPI_x_coord + 1, self.MPI_y_coord))
        else:
            self.MPI_rank_right = MPI.PROC_NULL
        if self.MPI_y_coord > 0 or self.periodic_y:
            self.MPI_rank_down = self.MPI_comm.Get_cart_rank(
                (self.MPI_x_coord, self.MPI_y_coord - 1))
        else:
            self.MPI_rank_down = MPI.PROC_NULL
        if self.MPI_y_coord < self.MPI_size_y - 1 or self.periodic_y:
            self.MPI_rank_up = self.MPI_comm.Get_cart_rank(
                (self.MPI_x_coord, self.MPI_y_coord + 1))
        else:
            self.MPI_rank_up = MPI.PROC_NULL

        self.processor_name = MPI.Get_processor_name()

        # Share out the points between processes in each direction:
        self.nx, nx_remaining = divmod(self.nx_global, self.MPI_size_x)
        if self.MPI_x_coord < nx_remaining:
            # Give the remaining to the lowest ranked processes:
            self.nx += 1
        self.ny, ny_remaining = divmod(self.ny_global, self.MPI_size_y)
        if self.MPI_y_coord < ny_remaining:
            # Give the remaining to the lowest ranked processes:
            self.ny += 1

        # What are our coordinates in the global array?
        self.global_first_x_index = self.nx * self.MPI_x_coord
        # Be sure to count the extra points the lower ranked processes have:
        if self.MPI_x_coord >= nx_remaining:
            self.global_first_x_index += nx_remaining

        self.global_first_y_index = self.ny * self.MPI_y_coord
        # Be sure to count the extra points the lower ranked processes have:
        if self.MPI_y_coord >= ny_remaining:
            self.global_first_y_index += ny_remaining

        # We need to tag our data to have a way other than rank to distinguish
        # between multiple messages the two tasks might be sending each other
        # at the same time:
        TAG_LEFT_TO_RIGHT = 0
        TAG_RIGHT_TO_LEFT = 1
        TAG_DOWN_TO_UP = 2
        TAG_UP_TO_DOWN = 3

        # Buffers and MPI request objects for sending and receiving data to
        # and from other processes. Sorted by whether the datatype is real or
        # complex.
        self.MPI_send_buffers = {}
        self.MPI_receive_buffers = {}
        self.MPI_requests = {}
        for dtype in [np.float64, np.complex128]:
            x_edge_shape = (self.n_edge_pts, self.ny)
            y_edge_shape = (self.nx, self.n_edge_pts)
            left_send_buffer = np.zeros(x_edge_shape, dtype=dtype)
            left_receive_buffer = np.zeros(x_edge_shape, dtype=dtype)
            right_send_buffer = np.zeros(x_edge_shape, dtype=dtype)
            right_receive_buffer = np.zeros(x_edge_shape, dtype=dtype)
            bottom_send_buffer = np.zeros(y_edge_shape, dtype=dtype)
            bottom_receive_buffer = np.zeros(y_edge_shape, dtype=dtype)
            top_send_buffer = np.zeros(y_edge_shape, dtype=dtype)
            top_receive_buffer = np.zeros(y_edge_shape, dtype=dtype)

            send_left = self.MPI_comm.Send_init(left_send_buffer,
                                                self.MPI_rank_left,
                                                tag=TAG_RIGHT_TO_LEFT)
            send_right = self.MPI_comm.Send_init(right_send_buffer,
                                                 self.MPI_rank_right,
                                                 tag=TAG_LEFT_TO_RIGHT)
            send_bottom = self.MPI_comm.Send_init(bottom_send_buffer,
                                                  self.MPI_rank_down,
                                                  tag=TAG_UP_TO_DOWN)
            send_top = self.MPI_comm.Send_init(top_send_buffer,
                                               self.MPI_rank_up,
                                               tag=TAG_DOWN_TO_UP)
            receive_left = self.MPI_comm.Recv_init(left_receive_buffer,
                                                   self.MPI_rank_left,
                                                   tag=TAG_LEFT_TO_RIGHT)
            receive_right = self.MPI_comm.Recv_init(right_receive_buffer,
                                                    self.MPI_rank_right,
                                                    tag=TAG_RIGHT_TO_LEFT)
            receive_bottom = self.MPI_comm.Recv_init(bottom_receive_buffer,
                                                     self.MPI_rank_down,
                                                     tag=TAG_DOWN_TO_UP)
            receive_top = self.MPI_comm.Recv_init(top_receive_buffer,
                                                  self.MPI_rank_up,
                                                  tag=TAG_UP_TO_DOWN)

            self.MPI_send_buffers[dtype] = (left_send_buffer,
                                            right_send_buffer,
                                            bottom_send_buffer,
                                            top_send_buffer)

            self.MPI_receive_buffers[dtype] = (left_receive_buffer,
                                               right_receive_buffer,
                                               bottom_receive_buffer,
                                               top_receive_buffer)

            self.MPI_requests[dtype] = (send_left, send_right, send_bottom,
                                        send_top, receive_left, receive_right,
                                        receive_bottom, receive_top)
        self.pending_requests = None
def mpiPI(nroProcesso, rank):#funcao que calcula o valor aprox de pi
    N = 840
    i = int(1 + (N/nroProcesso)*rank)
    k = int((N/nroProcesso)*(rank+1))
    somatorio = 0
    for j in range(i,k+1):
        somatorio += 1/(1+((j-0.5)/N)**2)
    #print(i,k)#intervalos
    #print((somatorio/N)*4)#somatorio de cada intervalo
    return (somatorio/N)*4

if __name__ == "__main__": #main -- Segunda versão
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()#rank do processo atual
    numDeProcessos = comm.Get_size()#numero de processos
    idmaquina = MPI.Get_processor_name()#hostname damaquina
    comm.Barrier()#barreira inicio
    tinicial = MPI.Wtime()
    res1 = mpiPI(comm.Get_size(),rank)
    comm.Barrier()#barreira fim
    tfinal=MPI.Wtime()
    k = ("Resposta do processo [" + str(rank) + "] = " + str(res1) + " ID Máquina = "+str(idmaquina))
    #print("-"*len(k)+"\n"+k+"\n")
    if rank == 0:
        bufferAux = [tfinal-tinicial]
        for i in range(1,numDeProcessos):
            bufferAux.append(comm.recv(source = i))
        arquivo.write(str(max(bufferAux))+"\n")
        arquivo.close()
        #print("Tempo de execução:",max(bufferAux))#tempo do processo que durou mais
    else:
Esempio n. 17
0
    """ Performs a reduction on valin across all processes in MPI_COMM_WORLD.
         Reduced value is stored in valout
         Reduction operation is controlled by rop:
         rop = MPI_SUM => Summation
         rop = MPI_MIN => Min
         rop = MPI_MAX => Max """
    valout = 0
    return valout


###########################
# Initialize Communication
cw = MPI.COMM_WORLD
rank = cw.rank
size = cw.size
proc = MPI.Get_processor_name()

#############################################
# Read problem parameters from command line

num = numpy.zeros(1, dtype='i')
if (rank == 0):
    try:
        num[0] = int(sys.argv[2])
    except:
        num[0] = 1

cw.Bcast(num, root=0)
op = num[0]

if (op > 3):
Esempio n. 18
0
import os
from mpi4py import MPI
import re
import time
import codecs
from analyse_tweet import analyseTweet, combineResults
from senticnet import *
from sentiwordnet import SentiWordNet
from sasa_tweet_classifier import SasaTweetClassifier
from nltk_tweet_classifier import NLTKTweetClassifier
from logger import Logger

SIZE = MPI.COMM_WORLD.Get_size()
RANK = MPI.COMM_WORLD.Get_rank()
NAME = MPI.Get_processor_name()
COMM = MPI.COMM_WORLD

if RANK == 0:
    log = Logger("logs/log_" + str(int(time.time())) + ".txt")
    log.log("MPI.COMM_WORLD.Get_size(): " + str(SIZE))
    totalTime = time.time()
    lapTime = time.time()
    log.log("Opening config file: config/config.txt")
    f = open('config/config.txt', 'r')
    config = f.read()
    config = config.split("#-#-#-#")
    filename = config[0]
    title = config[1]

    title_file_prefix = title.lower()
Esempio n. 19
0
def main():

    global TOTAL_SIMS_TO_RUN, PHOTONS_PER_BIN, SIZE_OF_WAVEBINS

    COMM = MPI.COMM_WORLD
    SIZE = COMM.Get_size()
    RANK = COMM.Get_rank()
    NAME = MPI.Get_processor_name()

    #for convenenience
    if TOTAL_SIMS_TO_RUN < 1:
        TOTAL_SIMS_TO_RUN = SIM_SAMPLES * SIZE  #give everyone SIM_SAMPLES, and take an average time for comparision

    #just for learning purposes
    #in this specific case, could skip this entirely and just have each worker use its RANK as the seed
    #as well as the total sims to run ... since this is previously agreed on (as a global constant)
    seed = None
    if (RANK == 0):
        # MPI start the clock
        walltime = MPI.Wtime()
        # print ("Start time:", walltime)

        #since the program has this defined, no need to actually communicate here
        #build the seeds:
        seed = np.arange(SIZE, dtype=int)

    #send everybody the total number to run
    COMM.bcast(TOTAL_SIMS_TO_RUN)

    #send each core its own seed
    seed = COMM.scatter(seed)

    #ALL cores (manager and workers) run simulations
    n_esc = run_all(RANK, TOTAL_SIMS_TO_RUN, SIZE, seed)

    #ALL cores send results to manager
    recvbuf = None
    if RANK == 0:
        recvbuf = np.empty([SIZE, SIZE_OF_WAVEBINS], dtype=int)

    #I think there is another way to do this ... like a Reduce call?

    #gather up everyone's n_esc
    COMM.Gather(n_esc, recvbuf, root=0)

    #sum up ... manager
    sum = []
    count = 0
    if RANK == 0:
        sum = np.zeros(SIZE_OF_WAVEBINS)
        for i in range(SIZE):
            if DEBUG_PRINT:
                print("Summing", i, recvbuf[i])
            sum += recvbuf[i]

        if DEBUG_PRINT:
            print("SUM", sum)

        sum = np.array(sum).astype(float)

        f_esc = sum / (PHOTONS_PER_BIN * TOTAL_SIMS_TO_RUN)

        if DEBUG_PRINT:
            print("f_esc", f_esc)

    if RANK == 0:
        # MPI stop the clock, get ellapsed time
        walltime = MPI.Wtime() - walltime

        print("Delta-time: ", walltime)
        print("   Per-sim: ", walltime / TOTAL_SIMS_TO_RUN)

    #todo: here is where we would make the plots, but don't bother since this is just
    #todo: a timing exercise

    if (RANK == 0):
        #sanity check .. make sure the escape curve matches the original data
        wavelengths = np.logspace(-2, 0.5, SIZE_OF_WAVEBINS)  # 10AA ~ 30,000AA
        plt.close('all')
        plt.plot(wavelengths, f_esc, label="mean f_esc")
        #plt.fill_between(wavelengths, f_esc_low, f_esc_high, color='k', alpha=0.3, label=r"1-$\sigma$")
        plt.xscale('log')
        plt.legend()
        plt.title("f_esc by wavelength (%d simulations)" % (TOTAL_SIMS_TO_RUN))
        plt.xlabel("wavelength bin [microns]")
        plt.ylabel("fraction of escaped photons")

        plt.savefig("rel_f_esc.png")

    MPI.Finalize()
####
################################################################
################################################################

from mpi4py import MPI
import numpy as np
import time

comm = MPI.COMM_WORLD

nprocs = comm.Get_size()
procno = comm.Get_rank()
ntests = 100

if procno==0 or procno==nprocs-1:
    print "Node name:",MPI.Get_processor_name()

## Determine who will be communicating:
## set processA, processB
#### your code here ####

##
## Do a range of test sizes
##

for s in [1,10,100,1000,10000,100000,1000000]:
    if procno==processA:
        ####
        #### Originating process:
        ####
        ##
Esempio n. 21
0
    def do_work(self, data):
        """do work method overrides Slave.do_work() and defines work
        to be done by every slave"""
        rank = MPI.COMM_WORLD.Get_rank()
        name = MPI.Get_processor_name()

        print('  Slave %s rank %d executing task %s' %
              (name, rank, data['task']))

        if data['task'] == 'initial_sim':
            # define explicit assimulo problem
            sim_obj = data['sim_obj']
            rhs_fun = sim_obj.rhs_fun  # data['rhs_fun']
            y_initial = data['y0']
            estimate_id = data['id']
            ode_opts = sim_obj.ode_opts  # data['ode_opts']
            ode_sys_opts = data['ode_sys_opts']
            t_final = sim_obj.t_final  # data['t_final']
            all_options = [ode_opts, ode_sys_opts]

            print(
                '  Slave %s rank %d executing initial_sim for estimate: %s sample: %s, data set: %s'
                % (name, rank, estimate_id[0], estimate_id[1], estimate_id[2]))
            slave_tout, slave_yout, _, _ = simulate_ode(rhs_fun,
                                                        y_initial,
                                                        tf=t_final,
                                                        opts=all_options)
            print(' ode simulation complete ')

            # calculate flux
            flux_fun = sim_obj.flux_fun  # data['flux_fun']
            slave_flux = np.array(
                list(map(lambda x: flux_fun(x, ode_sys_opts), slave_yout)))

            result = (slave_tout, slave_yout, slave_flux, estimate_id[0],
                      estimate_id[1], estimate_id[2], sim_obj, ode_sys_opts)

        elif data['task'] == 'perturbation_sim':

            sim_obj = data['sim_obj']
            rhs_fun = sim_obj.rhs_fun  # data['rhs_fun']
            y_initial = data['y0']
            estimate_id = data['id']
            perturbation_id = data['perturbation_id']
            ode_opts = sim_obj.ode_opts  # data['ode_opts']
            ode_sys_opts = data['ode_sys_opts']
            t_final = sim_obj.t_final  # data['t_final']
            all_options = [ode_opts, ode_sys_opts]

            print(
                '  Slave %s rank %d executing initial_sim for estimate: %s sample: %s, data set: %s '
                'perturbation: %s' %
                (name, rank, estimate_id[0], estimate_id[1], estimate_id[2],
                 perturbation_id))
            slave_tout, slave_yout, _, _ = simulate_ode(rhs_fun,
                                                        y_initial,
                                                        tf=t_final,
                                                        opts=all_options)
            print(' ode perturbation simulation complete ')

            # calculate flux
            flux_fun = sim_obj.flux_fun  # data['flux_fun']
            slave_flux = np.array(
                list(map(lambda x: flux_fun(x, ode_sys_opts), slave_yout)))

            result = (slave_tout, slave_yout, slave_flux, estimate_id[0],
                      estimate_id[1], estimate_id[2], perturbation_id)

        return data['task'], result
import os
if len(sys.argv) != 5:
    sys.stdout.write(
        'ERROR:\tUnexpected number of arguments.\nUSAGE:\tpython %s INPUT_PATH OUTPUT_PATH OVERWRITE(int) MPI(int)\n'
        % sys.argv[0])
    sys.exit(1)
this_dir = os.path.dirname(sys.argv[0])
inPath = sys.argv[1]
outPath = sys.argv[2]
overwrite = bool(int(sys.argv[3]))
mpi = bool(int(sys.argv[4]))
if mpi:
    from mpi4py import MPI
    nproc = MPI.COMM_WORLD.Get_size()
    iproc = MPI.COMM_WORLD.Get_rank()
    inode = MPI.Get_processor_name()
else:
    nproc, iproc, inode = 1, 0, 0
try:
    (_, _, f) = next(os.walk(inPath))
except StopIteration:
    sys.stdout.write('ERROR: Empty input directory.\n')
    sys.exit(1)
if mpi: fs = np.array_split(f, nproc)[iproc]
else: fs = f
for i, filename in enumerate(fs):
    outFile = os.path.join(outPath, filename)
    if os.path.isfile(outFile) and not overwrite:
        continue
    ascii_df = pd.read_csv(os.path.join(inPath, filename),
                           delim_whitespace=True,
Esempio n. 23
0
from mpi4py import MPI
import time
import os

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
host = MPI.Get_processor_name()
payload_size = 32
size = comm.Get_size()
msg_num = 0
status_ = MPI.Status()
has_run = False

if os.path.isfile('./results.csv') is False:
    with open('results.csv', 'a') as csv:
        ColumnRow = 'rank, msg_num, timestamp\n'
        csv.write(ColumnRow)
while msg_num < 100:
    with open('results.csv', 'a') as csv:
        if rank == 0:
            # print("Rank %s sending %d" % (rank, msg_num))
            data = bytes(payload_size)

            # send to mpi2
            time_send = time.time()
            comm.send(data, dest=1, tag=msg_num)
            csv.write('1, %s, %f' % (msg_num, time_send))
            csv.write('\n')

            # send to mpi3
            time_send = time.time()
Esempio n. 24
0
    def main(self):
        #  Initializations and preliminaries
        comm = MPI.COMM_WORLD   # get MPI communicator object
        size = comm.size        # total number of processes
        rank = comm.rank        # rank of this process
        status = MPI.Status()   # get MPI status object

        comm.Barrier()
        start_time = time.time()

        # Master process executes code below
        if rank == 0:
            num_workers = size - 1
            closed_workers = 0
            space = [self.spaceDict[key] for key in self.params]
            print("space: ", space)
            eval_counter = 0

            parDict = {}
            evalDict = {}
            resultsList = []
            parDict['kappa']=self.kappa
            init_x = []
            delta = 0.05
            #patience = max(100, 3 * num_workers-1)
            patience = len(self.params) * self.patience_fac
            last_imp = 0
            curr_best = math.inf

            if self.base_estimator =='NND':
                opt = Optimizer(space, base_estimator=NeuralNetworksDropoutRegressor(), acq_optimizer='sampling',
                            acq_func = self.acq_func, acq_func_kwargs=parDict, random_state=seed)
            else:
                opt = Optimizer(space,
                    base_estimator=self.base_estimator,
                    acq_optimizer=self.acq_optimizer,
                    acq_func=self.acq_func,
                    acq_func_kwargs=parDict,
                    random_state=seed,
                    n_initial_points=self.n_initial_points)
            print('Master starting with {} workers'.format(num_workers))

            while closed_workers < num_workers:
                data = comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
                source = status.Get_source()
                tag = status.Get_tag()
                elapsed_time = float(time.time() - start_time)
                print('elapsed_time:%1.3f'%elapsed_time)
                if tag == tags.READY:
                    if last_imp < patience and eval_counter < self.max_evals and elapsed_time < self.max_time:
                        if self.starting_point is not None:
                            x = self.starting_point
                            if num_workers-1 > 0:
                                init_x = opt.ask(n_points=num_workers-1)
                            self.starting_point = None
                        else:
                            if len(init_x) > 0:
                                x = init_x.pop(0)
                            else:
                                x = opt.ask(n_points=1, strategy='cl_min')[0]
                        key = str(x)
                        print('sample %s' % key)
                        if key in evalDict.keys():
                            print('%s already evalauted' % key)
                        evalDict[key] = None
                        task = {}
                        task['x'] = x
                        task['eval_counter'] = eval_counter
                        task['rank_master'] = rank
                        #task['start_time'] = elapsed_time
                        print('Sending task {} to worker {}'.format (eval_counter, source))
                        comm.send(task, dest=source, tag=tags.START)
                        eval_counter = eval_counter + 1
                    else:
                        comm.send(None, dest=source, tag=tags.EXIT)
                elif tag == tags.DONE:
                    result = data
                    result['end_time'] = elapsed_time
                    print('Got data from worker {}'.format(source))
                    resultsList.append(result)
                    x = result['x']
                    y = result['cost']
                    opt.tell(x, y)
                    percent_improv = -100*((y+0.1) - (curr_best+0.1))/(curr_best+0.1)
                    if y < curr_best:
                        if percent_improv >= delta or curr_best==math.inf:
                            curr_best = y
                            last_imp = 0
                    else:
                        last_imp = last_imp+1
                    print('curr_best={} percent_improv={} patience={}/{}'.format(curr_best, percent_improv, last_imp, patience))
                elif tag == tags.EXIT:
                    print('Worker {} exited.'.format(source))
                    closed_workers = closed_workers + 1
                    resultsList = data
                    print('Search finished..')
                    #resultsList = comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status) #comm.recv(source=MPI.ANY_SOURCE, tag=tags.EXIT)
                    #print(resultsList)
                    saveResults(resultsList, self.results_json_fname, self.results_csv_fname)
                    y_best = np.min(opt.yi)
                    best_index = np.where(opt.yi==y_best)[0][0]
                    x_best = opt.Xi[best_index]
                    print('Best: x = {}; y={}'.format(y_best, x_best))
        else:
            # Worker processes execute code below
            name = MPI.Get_processor_name()
            print("worker with rank %d on %s." % (rank, name))
            resultsList = []
            while True:
                comm.send(None, dest=0, tag=tags.READY)
                task = comm.recv(source=0, tag=MPI.ANY_TAG, status=status)
                tag = status.Get_tag()
                if tag == tags.START:
                    result = self.evaluate(self.problem, task, self.jobs_dir, self.results_dir)
                    elapsed_time = float(time.time() - start_time)
                    result['elapsed_time'] = elapsed_time
                    print(result)
                    resultsList.append(result)
                    comm.send(result, dest=0, tag=tags.DONE)
                elif tag == tags.EXIT:
                    print(f'Exit rank={comm.rank}')
                    break
            comm.send(resultsList, dest=0, tag=tags.EXIT)
Esempio n. 25
0
from mpi4py import MPI
import libyt
hwmess = 'Hello, World! I am process %d of %d on %s.'
myrank = MPI.COMM_WORLD.Get_rank()
nprocs = MPI.COMM_WORLD.Get_size()
procnm = MPI.Get_processor_name()
print(hwmess % (myrank, nprocs, procnm))

# print ("----------------------")
# print ("prop1 exist = %s" % ("prop1" in dir(libyt.libytSub)))
# print ("prop2 exist = %s" % ("prop2" in dir(libyt)))
# print ("prop3 exist = %s" % ("prop3" in dir(libyt)))
# print ("----------------------")
# print ("before yt_set_parameter(), libyt.prop1 = ", libyt.prop1)


def yt_inline():

    print(dir(libyt))
    #    print (libyt.dict.prop1)
    print("======================")
    a = libyt.fputs("Write to file : Here I am.", "write.txt")
    print("======================")
    print(libyt.prop1)


#    print (libyt.FPUTS_FLAG)
# print ("======================")
# print ("myrank = ", myrank)
# print ("after yt_set_parameter, libyt.prop1 = ", libyt.prop1)
# print ("libyt.prop3 = ", libyt.prop3)
Esempio n. 26
0
    #PartitionNum = 20

    DataPath = '/home/mapred/GraphData/uk/subdata/'
    VertexNum = 787803000
    PartitionNum = 3000

    #DataPath = '/home/mapred/GraphData/twitter/subdata/'
    #VertexNum = 41652250
    #PartitionNum = 50

    GraphInfo = (DataPath, VertexNum, PartitionNum, VertexNum / PartitionNum)
    test_graph = satgraph()

    rank_0_host = None
    if MPI.COMM_WORLD.Get_rank() == 0:
        rank_0_host = MPI.Get_processor_name()
    rank_0_host = MPI.COMM_WORLD.bcast(rank_0_host, root=0)

    test_graph.set_Dtype_All(Dtype_All)
    test_graph.set_GraphInfo(GraphInfo)
    test_graph.set_IP(rank_0_host)
    test_graph.set_port(18086)
    #test_graph.set_ThreadNum(1)
    test_graph.set_ThreadNum(4)
    test_graph.set_MaxIteration(50)
    test_graph.set_StaleNum(0)
    test_graph.set_FilterThreshold(0.000000001)
    test_graph.set_CalcFunc(calc_pagerank)

    test_graph.run('pagerank')
    os._exit(0)

if __name__ == '__main__':
    if len(sys.argv) != 6:
        sys.exit(
            'ERROR:\tPlease provide the path of the project directory.\nUSAGE:\t%s PROJECT_DIR CLIP_PREPROCESS? OUT_DIR_NAME OVERWRITE? PARALLEL?\n'
            % sys.argv[0])
    clip = bool(int(sys.argv[2]))
    outdir = sys.argv[3]
    overwrite = bool(int(sys.argv[4]))
    parallel = bool(int(sys.argv[5]))
    if parallel:
        from mpi4py import MPI
        size = MPI.COMM_WORLD.Get_size()  # Size of communicator
        rank = MPI.COMM_WORLD.Get_rank()  # Ranks in communicator
        name = MPI.Get_processor_name()  # Node where this MPI process runs
    WORKDIR = os.path.abspath(sys.argv[1])
    sys.stdout.write('Project directory: %s\n' % WORKDIR)
    SRC = os.path.join(WORKDIR, 'src')
    DATA = os.path.join(WORKDIR, 'data')
    RESULTS = os.path.join(WORKDIR, 'results')
    TRAIN = os.path.join(DATA, 'datapack2.0train/Public')
    TEST = os.path.join(DATA, 'datapack2.0test/Public')
    image_catalog = pd.read_csv(os.path.join(
        DATA, 'catalog/image_catalog2.0train.csv'),
                                comment='#',
                                index_col=0)
    image_catalog['is_lens'] = (image_catalog['mag_lens'] >
                                1.2) & (image_catalog['n_sources'] != 0)
    #image_catalog[['ID', 'is_lens']].to_csv(os.path.join(RESULTS, 'lens_id_labels.csv'), index=False)
    train_outpath = os.path.join(DATA, 'train_%s' % outdir)
        #print(line)
        if line[0] == '>': continue

        for i in line:
            #print(i)
            if i == 't':
                resultLine += 'a'
            elif i == 'a':
                resultLine += 'u'
            elif i == 'c':
                resultLine += 'g'
            elif i == 'g':
                resultLine += 'c'
            else:
                resultLine += i
        resultLine += '\n'
    resultado.write(resultLine)
    f.close()
    resultado.close()


start = datetime.now()
sys.stdout.flush()
main()
end = datetime.now()
total = end - start
tiempo_total = total.seconds
print("Procesador: " + MPI.Get_processor_name() + ', Timpo de ejecución:' +
      str(tiempo_total) + "s")
sys.stdout.flush()
Esempio n. 29
0
""" 6. Itaration over EQ, ST, Comp and frequency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
L = []
#for worker in range(size):
#
#    if worker==me :
for EQstCompFreq in LEqStCompFreq_split:
    #            # 6.0 take case feature EQ, station component and frequency
    EQname, Station, Year, jJul, Hour, Secondp, Seconds, Ml, Depth, Rdistance, Lat, Long, Az, BAz, Component, number, freqband = EQstCompFreq

    if Year <> f[EQname].attrs['Year'] or Hour <> f[EQname].attrs[
            'Hour'] or jJul <> f[EQname].attrs['JJul']:
        print 'ERROR PROBLEM OF EQ FEATURES', 'Je suis le worker {} et je traite le seisme {}, et la composante {} pour la frequence max {}'.format(
            me, EQname, Component, freqband[1])

    print 'I am the worker {} on the node {} delealing with Eq {} component {} and freq {}'.format(
        me, MPI.Get_processor_name(), EQname, Component, freqband[1])
    ## 6.1 read file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if not os.path.exists(
            '/home/burtin/DATA/LinTianShan/Seismic_Data/20%s/R%s.02/GSW0%s.%s.%s.%s.00.00.BHN.SAC'
            % (Year, jJul, Station, Year, jJul, str(Hour))):
        print 'file not existing Year :', Year, 'Julian day : ', jJul, 'Hour : ', Hour
        f[EQname]['St{}'.format(Station)]['Exist_{}'.format(
            Component)][0] = False
        continue

    st = Read_event(Year, jJul, Hour, Secondp, Station, False)
    #if the begining of the signal is too close to 00min then the wave peaking won't be accurate thus it's necessary to merge the signal whith the previous one!
    if Secondp < 100:
        if int(Hour) - 1 >= 0:
            st = st + Read_event(Year, jJul, str(int(Hour) - 1), Secondp,
                                 Station, False)
Esempio n. 30
0
from mpi4py import MPI
import sys
sys.path.insert(0, '//home/csunix/sc16ho/dev/miind/build/libs/PythonWrapper')
import libmiindpw

comm = MPI.COMM_WORLD
print "MASTER RANK: " + str(comm.Get_rank())

print MPI.Get_processor_name()

wrapped = libmiindpw.Wrapped()
wrapped.init()
wrapped.startSimulation()

x = [0.0 for a in range(76)]

while (1):
    x = wrapped.evolveSingleStep(x)