CoCalc Public FilesBoolean-Cayley-graphs / sage-code / save_psf_in_mpi_parallel.py
Author: Paul Leopardi
Compute Environment: Ubuntu 18.04 (Deprecated)
1r"""
2"""
3#*****************************************************************************
4#       Copyright (C) 2017 Paul Leopardi [email protected]
5#
7#  as published by the Free Software Foundation; either version 2 of
10#*****************************************************************************
11
12import sys
13
14from mpi4py import MPI
15from sage.all_cmdline import *
16
17from boolean_cayley_graphs.bent_function import BentFunction
18from boolean_cayley_graphs.classify_in_mpi_parallel import save_class_parts_in_parallel
19
20r"""
21"""
22# Check that the correct number of arguments exist.
23if len(sys.argv) < 4:
24    print("Usage: save_psf_in_mpi_parallel psf_seq bnbr fnbr c_len [dir]")
25    sys.exit(1)
26
27# Convert the arguments to int.
28seq_nbr  = int(sys.argv[1]) # Number of the partial spread function sequence
29fnbr0    = int(sys.argv[2]) # First function number within partial spread function sequence
30c_len    = int(sys.argv[3]) # Number of c values per class part.
31d = None
32if len(sys.argv) > 4:
33    d = sys.argv[4]         # Directory to save to
34
35# Get our MPI rank.
36comm = MPI.COMM_WORLD
37rank = comm.Get_rank()
38size = comm.Get_size()
39
40# Load the required bent function.
42psf_seq_name = "../psf/psf-"+str(seq_nbr)+".txt"
43psf_seq_file = open(psf_seq_name)
45
46# Obtain the first bent function to determine the dimension.
47bentf0 = BentFunction(anf_list[fnbr0])
48dim = bentf0.nvariables()
49v = 2 ** dim
50
51# This script assumes that the number of parts per bent function is
52# the Integer v // c_len, and the remainder v % c_len must be 0.
53nbr_parts_per_bentf, remainder_v = divmod(v, c_len)
54if remainder_v != 0:
55    print("c_len is not a factor of 2 ** dim. Remainder is", remainder_v)
56    exit(1)
57
58# This script also assumes that size is an integer multiple of
59# the number of parts per bent function.
60remainder_s = size % nbr_parts_per_bentf
61if remainder_s != 0:
62    print("nbr_parts_per_bentf is not a factor of size. Remainder is", remainder_s)
63    exit(1)
64
65# Obtain the correct bent function for this rank
66fnbr = fnbr0 + (rank // nbr_parts_per_bentf)
67bentf = BentFunction(anf_list[fnbr])
68
69# Save the classification in parts with c_len matrix rows each.
70save_class_parts_in_parallel(
71    comm,
72    "psf"+str(seq_nbr)+"_"+str(fnbr),
73    bentf,
74    c_len=c_len,
75    dir=d)
76sys.exit(0)
77