int main(void) { const char *na_info_string = NULL; char target_addr_string[PATH_MAX], *p; FILE *na_config = NULL; struct snappy_lookup_args snappy_lookup_args; hg_class_t *hg_class; hg_context_t *hg_context; hg_return_t hg_ret; /* Get info string */ na_info_string = getenv(HG_PORT_NAME); if (!na_info_string) { fprintf(stderr, HG_PORT_NAME " environment variable must be set"); exit(0); } printf("Using %s\n", na_info_string); /* Initialize Mercury with the desired network abstraction class */ hg_class = HG_Init(na_info_string, NA_FALSE); /* Create HG context */ hg_context = HG_Context_create(hg_class); /* The connection string is generated after NA_Addr_self()/NA_Addr_to_string(), * we must get that string and pass it to NA_Addr_lookup() */ na_config = fopen(TEMP_DIRECTORY CONFIG_FILE_NAME, "r"); if (!na_config) { fprintf(stderr, "Could not open config file from: %s\n", TEMP_DIRECTORY CONFIG_FILE_NAME); exit(0); } fgets(target_addr_string, PATH_MAX, na_config); p = strrchr(target_addr_string, '\n'); if (p != NULL) *p = '\0'; printf("Target address is: %s\n", target_addr_string); fclose(na_config); /* Look up target address */ snappy_lookup_args.hg_class = hg_class; snappy_lookup_args.hg_context = hg_context; HG_Addr_lookup(hg_context, snappy_lookup_cb, &snappy_lookup_args, target_addr_string, HG_OP_ID_IGNORE); /* Poke progress engine and check for events */ do { unsigned int actual_count = 0; do { hg_ret = HG_Trigger(hg_context, 0 /* timeout */, 1 /* max count */, &actual_count); } while ((hg_ret == HG_SUCCESS) && actual_count); /* Do not try to make progress anymore if we're done */ if (snappy_compress_done_g) break; hg_ret = HG_Progress(hg_context, HG_MAX_IDLE_TIME); } while (hg_ret == HG_SUCCESS); /* Finalize */ HG_Addr_free(hg_class, snappy_lookup_args.hg_target_addr); HG_Context_destroy(hg_context); HG_Finalize(hg_class); return EXIT_SUCCESS; }
/*---------------------------------------------------------------------------*/ static hg_return_t hg_test_drc_token_request(struct hg_test_info *hg_test_info) { hg_request_t *request = NULL; hg_handle_t handle; #ifdef HG_TEST_DRC_USE_TOKEN hg_string_t token; #else hg_uint32_t credential; #endif hg_test_drc_grant_in_t in_struct; hg_test_drc_grant_out_t out_struct; hg_return_t ret = HG_SUCCESS; #ifndef HG_TEST_DRC_IGNORE int rc; #endif /* Look up target addr using target name info */ ret = HG_Hl_addr_lookup_wait(hg_test_info->context, hg_test_info->request_class, hg_test_info->na_test_info.target_name, &hg_test_info->target_addr, HG_MAX_IDLE_TIME); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not find addr for target %s", hg_test_info->na_test_info.target_name); goto done; } /* Create new request */ request = hg_request_create(hg_test_info->request_class); /* Create request with invalid RPC id */ ret = HG_Create(hg_test_info->context, hg_test_info->target_addr, hg_test_drc_grant_id_g, &handle); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not create handle"); goto done; } /* Get WLM ID and set input */ #ifndef HG_TEST_DRC_IGNORE in_struct.wlm_id = drc_get_wlm_id(); #else in_struct.wlm_id = 12340; #endif /* Forward call to target addr */ printf("# %u requesting access to remote...\n", in_struct.wlm_id); fflush(stdout); ret = HG_Forward(handle, hg_test_drc_token_request_cb, request, &in_struct); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not forward call with id=%d", hg_test_drc_grant_id_g); goto done; } /* Wait for completion */ hg_request_wait(request, HG_MAX_IDLE_TIME, NULL); /* Get output */ ret = HG_Get_output(handle, &out_struct); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not get output"); goto done; } #ifdef HG_TEST_DRC_USE_TOKEN /* Get token back */ token = out_struct.token; printf("# Received token %s\n", token); fflush(stdout); /* Translate token */ #ifndef HG_TEST_DRC_IGNORE rc = drc_access_with_token(token, 0, &hg_test_info->credential_info); if (rc != DRC_SUCCESS) {/* failed to grant access to the credential */ HG_LOG_ERROR("drc_access_with_token() failed (%d, %s)", rc, drc_strerror(-rc)); ret = HG_PROTOCOL_ERROR; goto done; } #endif #else /* Get credential back */ credential = out_struct.credential; printf("# Received credential %u\n", credential); fflush(stdout); /* Access credential */ #ifndef HG_TEST_DRC_IGNORE drc_access_again: rc = drc_access(credential, 0, &hg_test_info->credential_info); if (rc != DRC_SUCCESS) { /* failed to access credential */ if (rc == -DRC_EINVAL) { sleep(1); goto drc_access_again; } HG_LOG_ERROR("drc_access() failed (%d, %s)", rc, drc_strerror(-rc)); ret = HG_PROTOCOL_ERROR; goto done; } #endif #endif /* Set cookie for further use */ #ifndef HG_TEST_DRC_IGNORE hg_test_info->cookie = drc_get_first_cookie(hg_test_info->credential_info); #else hg_test_info->cookie = 123456789; #endif printf("# Cookie is %u\n", hg_test_info->cookie); fflush(stdout); /* Clean up resources */ ret = HG_Free_output(handle, &out_struct); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not free output"); goto done; } ret = HG_Destroy(handle); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not destroy handle"); goto done; } hg_request_destroy(request); /* Free target addr */ ret = HG_Addr_free(hg_test_info->hg_class, hg_test_info->target_addr); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not free addr"); goto done; } done: return ret; }
/*---------------------------------------------------------------------------*/ hg_return_t hg_test_drc_acquire(int argc, char *argv[], struct hg_test_info *hg_test_info) { struct hg_test_info hg_test_drc_info = { 0 }; struct hg_init_info hg_test_drc_init_info = { 0 }; hg_return_t ret = HG_SUCCESS; if (!hg_test_info->credential) { /* Create an NA class with "tcp" protocol */ hg_test_drc_info.na_test_info.extern_init = NA_TRUE; hg_test_drc_info.na_test_info.protocol = strdup("tcp"); hg_test_drc_info.na_test_info.listen = hg_test_info->na_test_info.listen; if (NA_Test_init(argc, argv, &hg_test_drc_info.na_test_info) != NA_SUCCESS) { HG_LOG_ERROR("Could not initialize NA test layer"); ret = HG_NA_ERROR; goto done; } /* Assign NA class */ hg_test_drc_init_info.na_class = hg_test_drc_info.na_test_info.na_class; /* Init HG HL with init options */ ret = HG_Hl_init_opt(NULL, hg_test_drc_info.na_test_info.listen, &hg_test_drc_init_info); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not initialize HG HL"); goto done; } hg_test_drc_info.hg_class = HG_CLASS_DEFAULT; hg_test_drc_info.context = HG_CONTEXT_DEFAULT; hg_test_drc_info.request_class = HG_REQUEST_CLASS_DEFAULT; /* Attach test info to class */ HG_Class_set_data(hg_test_drc_info.hg_class, &hg_test_drc_info, NULL); /* Register routines */ hg_test_drc_register(hg_test_drc_info.hg_class); /* Acquire DRC token */ if (hg_test_drc_info.na_test_info.listen) { char addr_string[NA_TEST_MAX_ADDR_NAME]; na_size_t addr_string_len = NA_TEST_MAX_ADDR_NAME; hg_addr_t self_addr; ret = hg_test_drc_token_acquire(&hg_test_drc_info); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not acquire DRC token"); goto done; } /* TODO only rank 0 */ ret = HG_Addr_self(hg_test_drc_info.hg_class, &self_addr); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not get self addr"); goto done; } ret = HG_Addr_to_string(hg_test_drc_info.hg_class, addr_string, &addr_string_len, self_addr); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not convert addr to string"); goto done; } HG_Addr_free(hg_test_drc_info.hg_class, self_addr); na_test_set_config(addr_string); /* Used by CTest Test Driver to know when to launch clients */ MERCURY_TESTING_READY_MSG(); /* Progress */ do { unsigned int total_count = 0; unsigned int actual_count = 0; do { ret = HG_Trigger(hg_test_drc_info.context, 0, 1, &actual_count); total_count += actual_count; } while ((ret == HG_SUCCESS) && actual_count); /* Break as soon as something was triggered */ if (total_count) break; ret = HG_Progress(hg_test_drc_info.context, HG_MAX_IDLE_TIME); } while (ret == HG_SUCCESS || ret == HG_TIMEOUT); } else { char test_addr_name[NA_TEST_MAX_ADDR_NAME] = { '\0' }; if (hg_test_drc_info.na_test_info.mpi_comm_rank == 0) na_test_get_config(test_addr_name, NA_TEST_MAX_ADDR_NAME); /* Broadcast addr name */ NA_Test_bcast(test_addr_name, NA_TEST_MAX_ADDR_NAME, 0, &hg_test_drc_info.na_test_info); hg_test_drc_info.na_test_info.target_name = strdup(test_addr_name); printf("# Target name read: %s\n", hg_test_drc_info.na_test_info.target_name); ret = hg_test_drc_token_request(&hg_test_drc_info); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not request DRC token"); goto done; } } #ifdef MERCURY_HAS_PARALLEL_TESTING /* TODO bcast cookie when parallel mode */ #endif /* Finalize HG HL interface */ ret = HG_Hl_finalize(); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not finalize HG HL"); goto done; } /* Finalize NA test class interface */ #ifdef MERCURY_HAS_PARALLEL_TESTING hg_test_drc_info.na_test_info.mpi_no_finalize = NA_TRUE; #endif if (NA_Test_finalize(&hg_test_drc_info.na_test_info) != NA_SUCCESS) { HG_LOG_ERROR("Could not finalize NA test interface"); ret = HG_NA_ERROR; goto done; } hg_test_info->credential = hg_test_drc_info.credential; } else { hg_test_drc_info.credential = hg_test_info->credential; ret = hg_test_drc_token_acquire(&hg_test_drc_info); if (ret != HG_SUCCESS) { HG_LOG_ERROR("Could not acquire DRC token"); goto done; } } /* Copy cookie/credential info */ hg_test_info->wlm_id = hg_test_drc_info.wlm_id; hg_test_info->credential_info = hg_test_drc_info.credential_info; hg_test_info->cookie = hg_test_drc_info.cookie; /* Sleep a few seconds to make sure listener is initialized */ if (!hg_test_drc_info.na_test_info.listen) { unsigned int sleep_sec = 5; printf("# Sleeping now for %d seconds...\n", sleep_sec); fflush(stdout); sleep(sleep_sec); } done: return ret; }
/*---------------------------------------------------------------------------*/ hg_return_t HG_Test_finalize(hg_class_t *hg_class) { hg_return_t ret = HG_SUCCESS; na_return_t na_ret; NA_Test_barrier(); (void)hg_class; if (hg_test_is_client_g) { /* Terminate server */ if (hg_test_rank_g == 0) hg_test_finalize_rpc(); /* Free addr id */ ret = HG_Addr_free(HG_CLASS_DEFAULT, hg_test_addr_g); if (ret != HG_SUCCESS) { fprintf(stderr, "Could not free addr\n"); goto done; } hg_test_addr_g = HG_ADDR_NULL; } else if (hg_test_addr_table_g) { unsigned int i; for (i = 0; i < hg_test_addr_table_size_g; i++) HG_Addr_free(HG_CLASS_DEFAULT, hg_test_addr_table_g[i]); free(hg_test_addr_table_g); hg_test_addr_table_g = NULL; } /* Destroy bulk handle */ HG_Bulk_free(hg_test_local_bulk_handle_g); hg_test_local_bulk_handle_g = HG_BULK_NULL; #ifdef MERCURY_TESTING_HAS_THREAD_POOL hg_thread_pool_destroy(hg_test_thread_pool_g); hg_thread_mutex_destroy(&hg_test_local_bulk_handle_mutex_g); #endif /* Finalize interface */ ret = HG_Hl_finalize(); if (ret != HG_SUCCESS) { fprintf(stderr, "Could not finalize HG\n"); goto done; } na_ret = NA_Context_destroy(hg_test_na_class_g, hg_test_na_context_g); if (na_ret != NA_SUCCESS) { fprintf(stderr, "Could not destroy NA context\n"); goto done; } hg_test_na_context_g = NULL; na_ret = NA_Test_finalize(hg_test_na_class_g); if (na_ret != NA_SUCCESS) { fprintf(stderr, "Could not finalize NA interface\n"); goto done; } done: return ret; }