home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / test / io / bmi / bench-initialize.c < prev    next >
C/C++ Source or Header  |  2009-08-14  |  8KB  |  379 lines

  1. /*
  2.  * (C) 2001 Clemson University and The University of Chicago
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include "gossip.h"
  14. #include <mpi.h>
  15. #include "bmi.h"
  16. #include "bench-initialize.h"
  17. #include "bench-args.h"
  18. #include <strings.h>
  19.  
  20. int bench_init(
  21.     struct bench_options *opts,
  22.     int argc,
  23.     char *argv[],
  24.     int *num_clients,
  25.     int *world_rank,
  26.     MPI_Comm * comm,
  27.     PVFS_BMI_addr_t ** bmi_peer_array,
  28.     int **mpi_peer_array,
  29.     bmi_context_id * context)
  30. {
  31.     int ret = -1;
  32.     char local_proc_name[256];
  33.     int i = 0;
  34.  
  35.     /* start up MPI interface */
  36.     ret = MPI_Init(&argc, &argv);
  37.     if (ret != MPI_SUCCESS)
  38.     {
  39.     fprintf(stderr, "MPI_Init() failure.\n");
  40.     return (-1);
  41.     }
  42.  
  43.     /* parse command line arguments */
  44.     ret = bench_args(opts, argc, argv);
  45.     if (ret < 0)
  46.     {
  47.     fprintf(stderr, "bench_args() failure.\n");
  48.     return (-1);
  49.     }
  50.  
  51.     /* setup MPI parameters */
  52.     ret = bench_initialize_mpi_params(argc, argv, opts->num_servers,
  53.                       num_clients, world_rank, comm,
  54.                       local_proc_name);
  55.     if (ret < 0)
  56.     {
  57.     fprintf(stderr, "bench_initialize_mpi_params() failure.\n");
  58.     return (-1);
  59.     }
  60.  
  61.     /* startup BMI interface */
  62.     if (*world_rank < opts->num_servers)
  63.     {
  64.     *bmi_peer_array =
  65.         (PVFS_BMI_addr_t *) malloc((*num_clients) *
  66.                        sizeof(PVFS_BMI_addr_t));
  67.     *mpi_peer_array = (int *) malloc((*num_clients) * sizeof(int));
  68.     if (!(*bmi_peer_array) || !(*mpi_peer_array))
  69.     {
  70.         return (-1);
  71.     }
  72.     for (i = 0; i < (*num_clients); i++)
  73.     {
  74.         (*mpi_peer_array)[i] = i + opts->num_servers;
  75.     }
  76.     ret = bench_initialize_bmi_interface(opts->method_name,
  77.                          BMI_INIT_SERVER, context);
  78.     }
  79.     else
  80.     {
  81.     *bmi_peer_array =
  82.         (PVFS_BMI_addr_t *) malloc(opts->num_servers *
  83.                        sizeof(PVFS_BMI_addr_t));
  84.     *mpi_peer_array = (int *) malloc(opts->num_servers * sizeof(int));
  85.     if (!(*bmi_peer_array) || !(*mpi_peer_array))
  86.     {
  87.         return (-1);
  88.     }
  89.     for (i = 0; i < opts->num_servers; i++)
  90.     {
  91.         (*mpi_peer_array)[i] = i;
  92.     }
  93.     ret = bench_initialize_bmi_interface(opts->method_name, 0, context);
  94.     }
  95.     if (ret < 0)
  96.     {
  97.     fprintf(stderr, "bench_initialize_bmi_interface() failure.\n");
  98.     return (-1);
  99.     }
  100.  
  101.     MPI_Barrier(MPI_COMM_WORLD);
  102.  
  103.     /* gather BMI addresses */
  104.     if (*world_rank < opts->num_servers)
  105.     {
  106.     ret = bench_initialize_bmi_addresses_server(opts->num_servers,
  107.                             *num_clients,
  108.                             *bmi_peer_array,
  109.                             local_proc_name);
  110.     }
  111.     else
  112.     {
  113.     ret = bench_initialize_bmi_addresses_client(opts->num_servers,
  114.                             *num_clients,
  115.                             *bmi_peer_array,
  116.                             opts->method_name,
  117.                             *context);
  118.     }
  119.     if (ret < 0)
  120.     {
  121.     fprintf(stderr, "bench_initialize_bmi_addresses() failure.\n");
  122.     return (-1);
  123.     }
  124.  
  125.     return (0);
  126. }
  127.  
  128. int bench_initialize_bmi_interface(
  129.     char *method,
  130.     int flags,
  131.     bmi_context_id * context)
  132. {
  133.     char local_address[256];
  134.     int ret = -1;
  135.     const char *s;
  136.  
  137.     s = getenv("PVFS2_DEBUGMASK");
  138.     if (s)
  139.         gossip_set_debug_mask(1, PVFS_debug_eventlog_to_mask(s));
  140.     else
  141.         gossip_set_debug_mask(0, 0);
  142.  
  143.     s = getenv("PVFS2_DEBUGFILE");
  144.     if (s)
  145.         gossip_enable_file(s, "w");
  146.     else
  147.         gossip_enable_stderr();
  148.  
  149.     /* build the local listening address */
  150.     if (strcmp(method, "bmi_tcp") == 0)
  151.     {
  152.     sprintf(local_address, "tcp://NULL:%d\n", BMI_TCP_PORT);
  153.     }
  154.     else if (strcmp(method, "bmi_gm") == 0)
  155.     {
  156.     sprintf(local_address, "gm://NULL:%d\n", BMI_GM_PORT);
  157.     }
  158.     else if (strcmp(method, "bmi_mx") == 0)
  159.     {
  160.     sprintf(local_address, "mx://foo:0:%d\n", BMI_MX_ENDPOINT);
  161.     }
  162.     else if (strcmp(method, "bmi_ib") == 0)
  163.     {
  164.     sprintf(local_address, "ib://NULL:%d\n", BMI_IB_PORT);
  165.     }
  166.     else
  167.     {
  168.     fprintf(stderr, "Bad method: %s\n", method);
  169.     return (-1);
  170.     }
  171.  
  172.     if (flags & BMI_INIT_SERVER)
  173.     {
  174.     ret = BMI_initialize(NULL, NULL, 0);
  175.     }
  176.     else
  177.     {
  178.  
  179.     ret = BMI_initialize(NULL, NULL, 0);
  180.     }
  181.     if (ret < 0)
  182.     {
  183.     return (ret);
  184.     }
  185.  
  186.     ret = BMI_open_context(context);
  187.     return (ret);
  188.  
  189. }
  190.  
  191.  
  192. int bench_initialize_mpi_params(
  193.     int argc,
  194.     char **argv,
  195.     int num_servers,
  196.     int *num_clients,
  197.     int *world_rank,
  198.     MPI_Comm * comm,
  199.     char *local_proc_name)
  200. {
  201.     int ret = -1;
  202.     int numprocs, proc_namelen;
  203.     char *trunc_point = NULL;
  204.  
  205.     /* find out total # of processes & local id */
  206.     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  207.     MPI_Comm_rank(MPI_COMM_WORLD, world_rank);
  208.  
  209.     /* sanity check environment */
  210.     if (numprocs < 2)
  211.     {
  212.     fprintf(stderr, "bad number of procs.\n");
  213.     return (-1);
  214.     }
  215.     if (num_servers > (numprocs - 1))
  216.     {
  217.     fprintf(stderr, "bad number of servers.\n");
  218.     return (-1);
  219.     }
  220.  
  221.     *num_clients = numprocs - num_servers;
  222.  
  223.     /* put half of procs in server group, other half in client group */
  224.     if (*world_rank < num_servers)
  225.     {
  226.     ret = MPI_Comm_split(MPI_COMM_WORLD, 0, 0, comm);
  227.     }
  228.     else
  229.     {
  230.     ret = MPI_Comm_split(MPI_COMM_WORLD, 1, 0, comm);
  231.     }
  232.     if (ret != MPI_SUCCESS)
  233.     {
  234.     fprintf(stderr, "MPI_Comm_spit() failure.\n");
  235.     return (-1);
  236.     }
  237.  
  238.     /* record the name of this processor */
  239.     MPI_Get_processor_name(local_proc_name, &proc_namelen);
  240.  
  241.     /* trim off all but hostname portion */
  242.     trunc_point = index(local_proc_name, '.');
  243.     if (trunc_point)
  244.     {
  245.     trunc_point[0] = '\0';
  246.     }
  247.  
  248.     return (0);
  249. }
  250.  
  251. int bench_initialize_bmi_addresses_server(
  252.     int num_servers,
  253.     int num_clients,
  254.     PVFS_BMI_addr_t * client_array,
  255.     char *local_proc_name)
  256. {
  257.     int i = 0;
  258.     int ret = -1;
  259.     struct BMI_unexpected_info this_info;
  260.     int outcount = 0;
  261.  
  262.     /* send the name of the process to the clients */
  263.     for (i = 0; i < num_clients; i++)
  264.     {
  265.     ret = MPI_Send(local_proc_name, 256, MPI_BYTE, (num_servers + i), 0,
  266.                MPI_COMM_WORLD);
  267.     if (ret != MPI_SUCCESS)
  268.     {
  269.         return (-1);
  270.     }
  271.     }
  272.  
  273.     /* receive an unexpected message to acquire the BMI addresses and
  274.      * verify connectivity
  275.      */
  276.     for (i = 0; i < num_clients; i++)
  277.     {
  278.     do
  279.     {
  280.         ret = BMI_testunexpected(1, &outcount, &this_info, 0);
  281.     } while (ret == 0 && outcount == 0);
  282.     if (ret < 0)
  283.     {
  284.         return (-1);
  285.     }
  286.     client_array[i] = this_info.addr;
  287.     BMI_unexpected_free(this_info.addr, this_info.buffer);
  288.     }
  289.  
  290.     return (0);
  291. }
  292.  
  293. int bench_initialize_bmi_addresses_client(
  294.     int num_servers,
  295.     int num_clients,
  296.     PVFS_BMI_addr_t * server_array,
  297.     char *method_name,
  298.     bmi_context_id context)
  299. {
  300.     int i = 0;
  301.     int ret = -1;
  302.     char server_name[256];
  303.     char bmi_server_name[256];
  304.     bmi_op_id_t bmi_id;
  305.     int outcount, error_code;
  306.     bmi_size_t actual_size;
  307.     MPI_Status status_foo;
  308.  
  309.     /* receive all of the process names of the servers */
  310.     for (i = 0; i < num_servers; i++)
  311.     {
  312.     ret = MPI_Recv(server_name, 256, MPI_BYTE, i, 0, MPI_COMM_WORLD,
  313.                &status_foo);
  314.     if (ret != MPI_SUCCESS)
  315.     {
  316.         return (-1);
  317.     }
  318.     /* convert process nams into BMI addresses and lookup */
  319.     if (strcmp(method_name, "bmi_tcp") == 0)
  320.     {
  321.         sprintf(bmi_server_name, "tcp://%s:%d", server_name, BMI_TCP_PORT);
  322.     }
  323.     else if (strcmp(method_name, "bmi_gm") == 0)
  324.     {
  325.         sprintf(bmi_server_name, "gm://%s:%d", server_name, BMI_GM_PORT);
  326.     }
  327.     else if (strcmp(method_name, "bmi_mx") == 0)
  328.     {
  329.         sprintf(bmi_server_name, "mx://%s:0:%d", server_name, BMI_MX_ENDPOINT);
  330.     }
  331.     else if (strcmp(method_name, "bmi_ib") == 0)
  332.     {
  333.         sprintf(bmi_server_name, "ib://%s:%d", server_name, BMI_IB_PORT);
  334.     }
  335.     else
  336.     {
  337.         return (-1);
  338.     }
  339.     ret = BMI_addr_lookup(&server_array[i], bmi_server_name);
  340.     if (ret < 0)
  341.     {
  342.         return (-1);
  343.     }
  344.     }
  345.  
  346.     /* send an unexpected message to servers to inform them of client
  347.      * addresses and verify connectivity
  348.      */
  349.     for (i = 0; i < num_servers; i++)
  350.     {
  351.     ret = BMI_post_sendunexpected(&bmi_id, server_array[i], &ret,
  352.                       sizeof(int), BMI_EXT_ALLOC, 0, NULL,
  353.                       context, NULL);
  354.     if (ret == 0)
  355.     {
  356.         do
  357.         {
  358.         ret = BMI_test(bmi_id, &outcount, &error_code,
  359.                    &actual_size, NULL, 0, context);
  360.         } while (ret == 0 && outcount == 0);
  361.     }
  362.     if (ret < 0 || error_code != 0)
  363.     {
  364.         return (-1);
  365.     }
  366.     }
  367.  
  368.     return (0);
  369. }
  370.  
  371. /*
  372.  * Local variables:
  373.  *  c-indent-level: 4
  374.  *  c-basic-offset: 4
  375.  * End:
  376.  *
  377.  * vim: ts=8 sts=4 sw=4 expandtab
  378.  */
  379.