int main(int argc, char * argv[] ) { time_t now = time(0); int rank ,size; SRAssembler* srassembler = NULL; try { mpi_init(argc,argv); size=mpi_get_size(); rank=mpi_get_rank(); srassembler = SRAssembler::getInstance(rank); int ret = srassembler->init(argc, argv, rank, size); if (ret == -1) { throw -1; } srassembler->do_preprocessing(); srassembler->do_walking(); } catch (int e) { mpi_code code; code.action = ACTION_EXIT; code.value1 = 0; code.value2 = 0; mpi_bcast(get_mpi_code_value(code)); finalized(); return -1; } finalized(); if (rank == 0) { string str = "Execution time: " + int2str(time(0) - now) + " seconds"; srassembler->get_logger()->info(str); } return 0; }
/* * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first). * Caller must free the return string. * This function does return a 0 byte buffer with nbytes set to zero if the * value of A is zero. * * @a: a multi precision integer. * @nbytes: receives the length of this buffer. * @sign: if not NULL, it will be set to the sign of the a. * * Return: Pointer to MPI buffer or NULL on error */ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) { uint8_t *buf; unsigned int n; int ret; if (!nbytes) return NULL; n = mpi_get_size(a); if (!n) n++; buf = kmalloc(n, GFP_KERNEL); if (!buf) return NULL; ret = mpi_read_buffer(a, buf, n, nbytes, sign); if (ret) { kfree(buf); return NULL; } return buf; }
static int rsa_verify(struct akcipher_request *req) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); const struct rsa_key *pkey = rsa_get_key(tfm); MPI s, m = mpi_alloc(0); int ret = 0; int sign; if (!m) return -ENOMEM; if (unlikely(!pkey->n || !pkey->e)) { ret = -EINVAL; goto err_free_m; } if (req->dst_len < mpi_get_size(pkey->n)) { req->dst_len = mpi_get_size(pkey->n); ret = -EOVERFLOW; goto err_free_m; } ret = -ENOMEM; s = mpi_read_raw_from_sgl(req->src, req->src_len); if (!s) { ret = -ENOMEM; goto err_free_m; } ret = _rsa_verify(pkey, m, s); if (ret) goto err_free_s; ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); if (ret) goto err_free_s; if (sign < 0) ret = -EBADMSG; err_free_s: mpi_free(s); err_free_m: mpi_free(m); return ret; }
int rsa_get_d(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; key->d = mpi_read_raw_data(value, vlen); if (!key->d) return -ENOMEM; /* In FIPS mode only allow key size 2K & 3K */ if (fips_enabled && (mpi_get_size(key->d) != 256 || mpi_get_size(key->d) != 384)) { pr_err("RSA: key size not allowed in FIPS mode\n"); mpi_free(key->d); key->d = NULL; return -EINVAL; } return 0; }
static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { struct rsa_key *pkey = akcipher_tfm_ctx(tfm); int ret; ret = rsa_parse_priv_key(pkey, key, keylen); if (ret) return ret; if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { rsa_free_key(pkey); ret = -EINVAL; } return ret; }
/** * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) * * @a: a multi precision integer * @buf: bufer to which the output will be written to. Needs to be at * leaset mpi_get_size(a) long. * @buf_len: size of the buf. * @nbytes: receives the actual length of the data written on success and * the data to-be-written on -EOVERFLOW in case buf_len was too * small. * @sign: if not NULL, it will be set to the sign of a. * * Return: 0 on success or error code in case of error */ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, int *sign) { uint8_t *p; #if BYTES_PER_MPI_LIMB == 4 __be32 alimb; #elif BYTES_PER_MPI_LIMB == 8 __be64 alimb; #else #error please implement for this limb size. #endif unsigned int n = mpi_get_size(a); int i, lzeros; if (!buf || !nbytes) return -EINVAL; if (sign) *sign = a->sign; lzeros = count_lzeros(a); if (buf_len < n - lzeros) { *nbytes = n - lzeros; return -EOVERFLOW; } p = buf; *nbytes = n - lzeros; for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, lzeros %= BYTES_PER_MPI_LIMB; i >= 0; i--) { #if BYTES_PER_MPI_LIMB == 4 alimb = cpu_to_be32(a->d[i]); #elif BYTES_PER_MPI_LIMB == 8 alimb = cpu_to_be64(a->d[i]); #else #error please implement for this limb size. #endif memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros); p += BYTES_PER_MPI_LIMB - lzeros; lzeros = 0; } return 0; }
static int dh_max_size(struct crypto_kpp *tfm) { struct dh_ctx *ctx = dh_get_ctx(tfm); return mpi_get_size(ctx->p); }
int main(int argc, char **argv) { //set the mpi settings int threadprovided; MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &threadprovided); if(threadprovided != MPI_THREAD_MULTIPLE) { printf("MPI multiple thread isn't provided!\n"); fflush(stdout); mpi_exit(1); } int current_rank = mpi_get_rank(); int nr_ranks = mpi_get_size(); param.nr_ranks = nr_ranks; char hostname[1024]; int hostname_len; MPI_Get_processor_name(hostname, &hostname_len); printf("processor name: %s, number of processed: %d, rank: %d\n", hostname, nr_ranks, current_rank); fflush(stdout); // int global_l; char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); //set the number of threads for the shared-memory system int nr_threads = param.thread_count; int max_thread_count = omp_get_max_threads(); if(nr_threads > max_thread_count) { printf("[rank %d], please enter the correct number of threads: 1~%d\n", current_rank, max_thread_count); mpi_exit(1); } omp_set_num_threads(nr_threads); //set the cpu affnity /*int ithread, err, cpu; cpu_set_t cpu_mask; #pragma omp parallel private(ithread, cpu_mask, err, cpu) { ithread = omp_get_thread_num(); CPU_ZERO(&cpu_mask);//set mask to zero CPU_SET(ithread, &cpu_mask);//set mask with ithread err = sched_setaffinity((pid_t)0, sizeof(cpu_mask), &cpu_mask); cpu = sched_getcpu(); printf("thread_id %d on CPU %d\n", ithread, cpu); }*/ //now, read the problem from the input file read_problem(input_file_name); error_msg = rksvm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); mpi_exit(1); } //distributed code global_l = prob.l; mpi_allreduce(&global_l, 1, MPI_INT, MPI_SUM);//MPI_INT :int;MPI_SUM:sum prob.global_l = global_l; printf("#local instances = %d, #global instances = %d\n", prob.l, prob.global_l); fflush(stdout); if(current_rank==0){ puts("Start to train!"); } model = rksvm_train(&prob,¶m); if(rksvm_save_model(model_file_name,model)) { fprintf(stderr,"[rank %d] can't save model to file %s\n",mpi_get_rank(), model_file_name); mpi_exit(1); } rksvm_free_and_destroy_model(&model); free(prob.y); free(prob.x); free(prob.query); free(x_space); free(prob.length_of_each_rksvm_node); free(line); MPI_Finalize(); return 0; }
/** * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first) * * This function works in the same way as the mpi_read_buffer, but it * takes an sgl instead of u8 * buf. * * @a: a multi precision integer * @sgl: scatterlist to write to. Needs to be at least * mpi_get_size(a) long. * @nbytes: in/out param - it has the be set to the maximum number of * bytes that can be written to sgl. This has to be at least * the size of the integer a. On return it receives the actual * length of the data written. * @sign: if not NULL, it will be set to the sign of a. * * Return: 0 on success or error code in case of error */ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, int *sign) { u8 *p, *p2; mpi_limb_t alimb, alimb2; unsigned int n = mpi_get_size(a); int i, x, y = 0, lzeros = 0, buf_len; if (!nbytes || *nbytes < n) return -EINVAL; if (sign) *sign = a->sign; p = (void *)&a->d[a->nlimbs] - 1; for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) { if (!*p) lzeros++; else break; } *nbytes = n - lzeros; buf_len = sgl->length; p2 = sg_virt(sgl); for (i = a->nlimbs - 1; i >= 0; i--) { alimb = a->d[i]; p = (u8 *)&alimb2; #if BYTES_PER_MPI_LIMB == 4 *p++ = alimb >> 24; *p++ = alimb >> 16; *p++ = alimb >> 8; *p++ = alimb; #elif BYTES_PER_MPI_LIMB == 8 *p++ = alimb >> 56; *p++ = alimb >> 48; *p++ = alimb >> 40; *p++ = alimb >> 32; *p++ = alimb >> 24; *p++ = alimb >> 16; *p++ = alimb >> 8; *p++ = alimb; #else #error please implement for this limb size. #endif if (lzeros > 0) { if (lzeros >= sizeof(alimb)) { p -= sizeof(alimb); continue; } else { mpi_limb_t *limb1 = (void *)p - sizeof(alimb); mpi_limb_t *limb2 = (void *)p - sizeof(alimb) + lzeros; *limb1 = *limb2; p -= lzeros; y = lzeros; } lzeros -= sizeof(alimb); } p = p - (sizeof(alimb) - y); for (x = 0; x < sizeof(alimb) - y; x++) { if (!buf_len) { sgl = sg_next(sgl); if (!sgl) return -EINVAL; buf_len = sgl->length; p2 = sg_virt(sgl); } *p2++ = *p++; buf_len--; } y = 0; } return 0; }
/** * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) * * @a: a multi precision integer * @buf: bufer to which the output will be written to. Needs to be at * leaset mpi_get_size(a) long. * @buf_len: size of the buf. * @nbytes: receives the actual length of the data written. * @sign: if not NULL, it will be set to the sign of a. * * Return: 0 on success or error code in case of error */ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, int *sign) { uint8_t *p; mpi_limb_t alimb; unsigned int n = mpi_get_size(a); int i, lzeros = 0; if (buf_len < n || !buf || !nbytes) return -EINVAL; if (sign) *sign = a->sign; p = (void *)&a->d[a->nlimbs] - 1; for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) { if (!*p) lzeros++; else break; } p = buf; *nbytes = n - lzeros; for (i = a->nlimbs - 1; i >= 0; i--) { alimb = a->d[i]; #if BYTES_PER_MPI_LIMB == 4 *p++ = alimb >> 24; *p++ = alimb >> 16; *p++ = alimb >> 8; *p++ = alimb; #elif BYTES_PER_MPI_LIMB == 8 *p++ = alimb >> 56; *p++ = alimb >> 48; *p++ = alimb >> 40; *p++ = alimb >> 32; *p++ = alimb >> 24; *p++ = alimb >> 16; *p++ = alimb >> 8; *p++ = alimb; #else #error please implement for this limb size. #endif if (lzeros > 0) { if (lzeros >= sizeof(alimb)) { p -= sizeof(alimb); } else { mpi_limb_t *limb1 = (void *)p - sizeof(alimb); mpi_limb_t *limb2 = (void *)p - sizeof(alimb) + lzeros; *limb1 = *limb2; p -= lzeros; } lzeros -= sizeof(alimb); } } return 0; }
/** * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first) * * This function works in the same way as the mpi_read_buffer, but it * takes an sgl instead of u8 * buf. * * @a: a multi precision integer * @sgl: scatterlist to write to. Needs to be at least * mpi_get_size(a) long. * @nbytes: in/out param - it has the be set to the maximum number of * bytes that can be written to sgl. This has to be at least * the size of the integer a. On return it receives the actual * length of the data written on success or the data that would * be written if buffer was too small. * @sign: if not NULL, it will be set to the sign of a. * * Return: 0 on success or error code in case of error */ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, int *sign) { u8 *p, *p2; #if BYTES_PER_MPI_LIMB == 4 __be32 alimb; #elif BYTES_PER_MPI_LIMB == 8 __be64 alimb; #else #error please implement for this limb size. #endif unsigned int n = mpi_get_size(a); int i, x, y = 0, lzeros, buf_len; if (!nbytes) return -EINVAL; if (sign) *sign = a->sign; lzeros = count_lzeros(a); if (*nbytes < n - lzeros) { *nbytes = n - lzeros; return -EOVERFLOW; } *nbytes = n - lzeros; buf_len = sgl->length; p2 = sg_virt(sgl); for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, lzeros %= BYTES_PER_MPI_LIMB; i >= 0; i--) { #if BYTES_PER_MPI_LIMB == 4 alimb = cpu_to_be32(a->d[i]); #elif BYTES_PER_MPI_LIMB == 8 alimb = cpu_to_be64(a->d[i]); #else #error please implement for this limb size. #endif if (lzeros) { y = lzeros; lzeros = 0; } p = (u8 *)&alimb + y; for (x = 0; x < sizeof(alimb) - y; x++) { if (!buf_len) { sgl = sg_next(sgl); if (!sgl) return -EINVAL; buf_len = sgl->length; p2 = sg_virt(sgl); } *p2++ = *p++; buf_len--; } y = 0; } return 0; }
static int rsa_max_size(struct crypto_akcipher *tfm) { struct rsa_key *pkey = akcipher_tfm_ctx(tfm); return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; }