dispatch_source_t create_source(dispatch_source_type_t type, int socket, int action) { log_detail("make source socket %d action %s\n", socket, action_name(action)); dispatch_source_t source = dispatch_source_create(type, socket, 0, queue); dispatch_source_set_event_handler(source, ^{ log_detail("source event socket %d action %s\n", socket, action_name(action)); curl_perform_action(socket, action); });
curl_context_t* create_curl_context() { curl_context_t *context = (curl_context_t *) malloc(sizeof *context); memset(context, 0, sizeof(curl_context_t)); log_detail("created context %p\n", context); return context; }
/* * Create directory with error log message when failing */ bool create_dir(char *path) { if (mkdir_p(path, 0700) == 0) return true; log_error(_("unable to create directory \"%s\""), path); log_detail("%s", strerror(errno)); return false; }
void destroy_curl_context(curl_context_t *context) { if (context->read_source) dispatch_source_cancel(context->read_source); if (context->write_source) dispatch_source_cancel(context->write_source); log_detail("destroyed context %p\n", context); free(context); }
/* * do_bdr_register() * * As each BDR node is its own primary, registering a BDR node * will create the repmgr metadata schema if necessary. */ void do_bdr_register(void) { PGconn *conn = NULL; BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; ExtensionStatus extension_status = REPMGR_UNKNOWN; t_node_info node_info = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; PQExpBufferData event_details; bool success = true; char *dbname = NULL; /* sanity-check configuration for BDR-compatability */ if (config_file_options.replication_type != REPLICATION_TYPE_BDR) { log_error(_("cannot run BDR REGISTER on a non-BDR node")); exit(ERR_BAD_CONFIG); } dbname = pg_malloc0(MAXLEN); if (dbname == NULL) { log_error(_("unable to allocate memory; terminating.")); exit(ERR_OUT_OF_MEMORY); } /* store the database name for future reference */ get_conninfo_value(config_file_options.conninfo, "dbname", dbname); conn = establish_db_connection(config_file_options.conninfo, true); if (!is_bdr_db(conn, NULL)) { log_error(_("database \"%s\" is not BDR-enabled"), dbname); log_hint(_("when using repmgr with BDR, the repmgr schema must be stored in the BDR database")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* Check that there are at most 2 BDR nodes */ get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("database \"%s\" is BDR-enabled but no BDR nodes were found"), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* BDR 2 implementation is for 2 nodes only */ if (get_bdr_version_num() < 3 && bdr_nodes.node_count > 2) { log_error(_("repmgr can only support BDR 2.x clusters with 2 nodes")); log_detail(_("this BDR cluster has %i nodes"), bdr_nodes.node_count); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (get_bdr_version_num() > 2) { log_error(_("\"repmgr bdr register\" is for BDR 2.x only")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* check for a matching BDR node */ { PQExpBufferData bdr_local_node_name; bool node_match = false; initPQExpBuffer(&bdr_local_node_name); node_match = bdr_node_name_matches(conn, config_file_options.node_name, &bdr_local_node_name); if (node_match == false) { if (strlen(bdr_local_node_name.data)) { log_error(_("local node BDR node name is \"%s\", expected: \"%s\""), bdr_local_node_name.data, config_file_options.node_name); log_hint(_("\"node_name\" in repmgr.conf must match \"node_name\" in bdr.bdr_nodes")); } else { log_error(_("local node does not report BDR node name")); log_hint(_("ensure this is an active BDR node")); } PQfinish(conn); pfree(dbname); termPQExpBuffer(&bdr_local_node_name); exit(ERR_BAD_CONFIG); } termPQExpBuffer(&bdr_local_node_name); } /* check whether repmgr extension exists, and there are no non-BDR nodes registered */ extension_status = get_repmgr_extension_status(conn, NULL); if (extension_status == REPMGR_UNKNOWN) { log_error(_("unable to determine status of \"repmgr\" extension in database \"%s\""), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_UNAVAILABLE) { log_error(_("\"repmgr\" extension is not available")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_INSTALLED) { if (!is_bdr_repmgr(conn)) { log_error(_("repmgr metadatabase contains records for non-BDR nodes")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } } else { log_debug("creating repmgr extension in database \"%s\"", dbname); begin_transaction(conn); if (!create_repmgr_extension(conn)) { log_error(_("unable to create repmgr extension - see preceding error message(s); aborting")); rollback_transaction(conn); pfree(dbname); PQfinish(conn); exit(ERR_BAD_CONFIG); } commit_transaction(conn); } pfree(dbname); if (bdr_node_has_repmgr_set(conn, config_file_options.node_name) == false) { log_debug("bdr_node_has_repmgr_set() = false"); bdr_node_set_repmgr_set(conn, config_file_options.node_name); } /* * before adding the extension tables to the replication set, if any other * BDR nodes exist, populate repmgr.nodes with a copy of existing entries * * currently we won't copy the contents of any other tables * */ { NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER; (void) get_all_node_records(conn, &local_node_records); if (local_node_records.node_count == 0) { BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; BdrNodeInfoListCell *bdr_cell = NULL; get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("unable to retrieve any BDR node records")); log_detail("%s", PQerrorMessage(conn)); PQfinish(conn); exit(ERR_BAD_CONFIG); } for (bdr_cell = bdr_nodes.head; bdr_cell; bdr_cell = bdr_cell->next) { PGconn *bdr_node_conn = NULL; NodeInfoList existing_nodes = T_NODE_INFO_LIST_INITIALIZER; NodeInfoListCell *cell = NULL; ExtensionStatus other_node_extension_status = REPMGR_UNKNOWN; /* skip the local node */ if (strncmp(node_info.node_name, bdr_cell->node_info->node_name, sizeof(node_info.node_name)) == 0) { continue; } log_debug("connecting to BDR node \"%s\" (conninfo: \"%s\")", bdr_cell->node_info->node_name, bdr_cell->node_info->node_local_dsn); bdr_node_conn = establish_db_connection_quiet(bdr_cell->node_info->node_local_dsn); if (PQstatus(bdr_node_conn) != CONNECTION_OK) { continue; } /* check repmgr schema exists, skip if not */ other_node_extension_status = get_repmgr_extension_status(bdr_node_conn, NULL); if (other_node_extension_status != REPMGR_INSTALLED) { continue; } (void) get_all_node_records(bdr_node_conn, &existing_nodes); for (cell = existing_nodes.head; cell; cell = cell->next) { log_debug("creating record for node \"%s\" (ID: %i)", cell->node_info->node_name, cell->node_info->node_id); create_node_record(conn, "bdr register", cell->node_info); } PQfinish(bdr_node_conn); break; } } } /* Add the repmgr extension tables to a replication set */ if (get_bdr_version_num() < 3) { add_extension_tables_to_bdr_replication_set(conn); } else { /* this is the only table we need to replicate */ char *replication_set = get_default_bdr_replication_set(conn); /* * this probably won't happen, but we need to be sure we're using * the replication set metadata correctly... */ if (conn == NULL) { log_error(_("unable to retrieve default BDR replication set")); log_hint(_("see preceding messages")); log_debug("check query in get_default_bdr_replication_set()"); exit(ERR_BAD_CONFIG); } if (is_table_in_bdr_replication_set(conn, "nodes", replication_set) == false) { add_table_to_bdr_replication_set(conn, "nodes", replication_set); } pfree(replication_set); } initPQExpBuffer(&event_details); begin_transaction(conn); /* * we'll check if a record exists (even if the schema was just created), * as there's a faint chance of a race condition */ record_status = get_node_record(conn, config_file_options.node_id, &node_info); /* Update internal node record */ node_info.type = BDR; node_info.node_id = config_file_options.node_id; node_info.upstream_node_id = NO_UPSTREAM_NODE; node_info.active = true; node_info.priority = config_file_options.priority; strncpy(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)); strncpy(node_info.location, config_file_options.location, sizeof(node_info.location)); strncpy(node_info.conninfo, config_file_options.conninfo, sizeof(node_info.conninfo)); if (record_status == RECORD_FOUND) { bool node_updated = false; /* * At this point we will have established there are no non-BDR * records, so no need to verify the node type */ if (!runtime_options.force) { log_error(_("this node is already registered")); log_hint(_("use -F/--force to overwrite the existing node record")); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } /* * don't permit changing the node name - this must match the BDR node * name set when the node was registered. */ if (strncmp(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)) != 0) { log_error(_("a record for node %i is already registered with node_name \"%s\""), config_file_options.node_id, node_info.node_name); log_hint(_("node_name configured in repmgr.conf is \"%s\""), config_file_options.node_name); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } node_updated = update_node_record(conn, "bdr register", &node_info); if (node_updated == true) { appendPQExpBuffer(&event_details, _("node record updated for node \"%s\" (%i)"), config_file_options.node_name, config_file_options.node_id); log_verbose(LOG_NOTICE, "%s", event_details.data); } else { success = false; } } else { /* create new node record */ bool node_created = create_node_record(conn, "bdr register", &node_info); if (node_created == true) { appendPQExpBuffer(&event_details, _("node record created for node \"%s\" (ID: %i)"), config_file_options.node_name, config_file_options.node_id); log_notice("%s", event_details.data); } else { success = false; } } if (success == false) { rollback_transaction(conn); PQfinish(conn); exit(ERR_DB_QUERY); } commit_transaction(conn); /* Log the event */ create_event_notification( conn, &config_file_options, config_file_options.node_id, "bdr_register", true, event_details.data); termPQExpBuffer(&event_details); PQfinish(conn); log_notice(_("BDR node %i registered (conninfo: %s)"), config_file_options.node_id, config_file_options.conninfo); return; }
pid_t enable_wal_receiver(PGconn *conn, bool wait_startup) { char buf[MAXLEN]; int wal_retrieve_retry_interval; pid_t wal_receiver_pid = UNKNOWN_PID; /* make timeout configurable */ int i, timeout = 30; if (is_superuser_connection(conn, NULL) == false) { log_error(_("superuser connection required")); return UNKNOWN_PID; } if (get_recovery_type(conn) == RECTYPE_PRIMARY) { log_error(_("node is not in recovery")); log_detail(_("wal receiver can only run on standby nodes")); return UNKNOWN_PID; } if (get_pg_setting(conn, "wal_retrieve_retry_interval", buf) == false) { log_error(_("unable to retrieve \"wal_retrieve_retry_interval\"")); return UNKNOWN_PID; } /* TODO: potentially handle atoi error, though unlikely at this point */ wal_retrieve_retry_interval = atoi(buf); if (wal_retrieve_retry_interval > WALRECEIVER_DISABLE_TIMEOUT_VALUE) { int new_wal_retrieve_retry_interval = wal_retrieve_retry_interval - WALRECEIVER_DISABLE_TIMEOUT_VALUE; bool success; log_notice(_("setting \"wal_retrieve_retry_interval\" to %i ms"), new_wal_retrieve_retry_interval); success = alter_system_int(conn, "wal_retrieve_retry_interval", new_wal_retrieve_retry_interval); if (success == false) { log_warning(_("unable to change \"wal_retrieve_retry_interval\"")); return UNKNOWN_PID; } pg_reload_conf(conn); } else { /* TODO: add threshold sanity check */ log_info(_("\"wal_retrieve_retry_interval\" is %i, not changing"), wal_retrieve_retry_interval); } if (wait_startup == false) return UNKNOWN_PID; for (i = 0; i < timeout; i++) { wal_receiver_pid = (pid_t)get_wal_receiver_pid(conn); if (wal_receiver_pid > 0) break; log_info(_("sleeping %i of maximum %i seconds waiting for WAL receiver to start up"), i + 1, timeout) sleep(1); } if (wal_receiver_pid == UNKNOWN_PID) { log_warning(_("unable to retrieve WAL receiver PID")); return UNKNOWN_PID; } else if (wal_receiver_pid == 0) { log_error(_("WAL receiver did not start up after %i seconds"), timeout); return UNKNOWN_PID; } log_info(_("WAL receiver started up with PID %i"), (int)wal_receiver_pid); return wal_receiver_pid; }
pid_t disable_wal_receiver(PGconn *conn) { char buf[MAXLEN]; int wal_retrieve_retry_interval, new_wal_retrieve_retry_interval; pid_t wal_receiver_pid = UNKNOWN_PID; int kill_ret; int i, j; int max_retries = 2; if (is_superuser_connection(conn, NULL) == false) { log_error(_("superuser connection required")); return UNKNOWN_PID; } if (get_recovery_type(conn) == RECTYPE_PRIMARY) { log_error(_("node is not in recovery")); log_detail(_("wal receiver can only run on standby nodes")); return UNKNOWN_PID; } wal_receiver_pid = (pid_t)get_wal_receiver_pid(conn); if (wal_receiver_pid == UNKNOWN_PID) { log_warning(_("unable to retrieve wal receiver PID")); return UNKNOWN_PID; } get_pg_setting(conn, "wal_retrieve_retry_interval", buf); /* TODO: potentially handle atoi error, though unlikely at this point */ wal_retrieve_retry_interval = atoi(buf); new_wal_retrieve_retry_interval = wal_retrieve_retry_interval + WALRECEIVER_DISABLE_TIMEOUT_VALUE; if (wal_retrieve_retry_interval < WALRECEIVER_DISABLE_TIMEOUT_VALUE) { log_notice(_("setting \"wal_retrieve_retry_interval\" to %i milliseconds"), new_wal_retrieve_retry_interval); alter_system_int(conn, "wal_retrieve_retry_interval", new_wal_retrieve_retry_interval); pg_reload_conf(conn); } /* * If, at this point, the WAL receiver is not running, we don't need to (and indeed can't) * kill it. */ if (wal_receiver_pid == 0) { log_warning(_("wal receiver not running")); return UNKNOWN_PID; } /* why 5? */ log_info(_("sleeping 5 seconds")); sleep(5); /* see comment below as to why we need a loop here */ for (i = 0; i < max_retries; i++) { log_notice(_("killing WAL receiver with PID %i"), (int)wal_receiver_pid); kill((int)wal_receiver_pid, SIGTERM); for (j = 0; j < 30; j++) { kill_ret = kill(wal_receiver_pid, 0); if (kill_ret != 0) { log_info(_("WAL receiver with pid %i killed"), (int)wal_receiver_pid); break; } sleep(1); } /* * Wait briefly to check that the WAL receiver has indeed gone away - * for reasons as yet unclear, after a server start/restart, immediately * after the first time a WAL receiver is killed, a new one is started * straight away, so we'll need to kill that too. */ sleep(1); wal_receiver_pid = (pid_t)get_wal_receiver_pid(conn); if (wal_receiver_pid == UNKNOWN_PID || wal_receiver_pid == 0) break; } return wal_receiver_pid; }
bool create_pg_dir(char *path, bool force) { /* Check this directory can be used as a PGDATA dir */ switch (check_dir(path)) { case DIR_NOENT: /* directory does not exist, attempt to create it */ log_info(_("creating directory \"%s\"..."), path); if (!create_dir(path)) { log_error(_("unable to create directory \"%s\"..."), path); return false; } break; case DIR_EMPTY: /* exists but empty, fix permissions and use it */ log_info(_("checking and correcting permissions on existing directory \"%s\""), path); if (!set_dir_permissions(path)) { log_error(_("unable to change permissions of directory \"%s\""), path); log_detail("%s", strerror(errno)); return false; } break; case DIR_NOT_EMPTY: /* exists but is not empty */ log_warning(_("directory \"%s\" exists but is not empty"), path); if (is_pg_dir(path)) { if (force == true) { log_notice(_("-F/--force provided - deleting existing data directory \"%s\""), path); nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS); return true; } return false; } else { if (force == true) { log_notice(_("deleting existing directory \"%s\""), path); nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS); return true; } return false; } break; case DIR_ERROR: log_error(_("could not access directory \"%s\": %s"), path, strerror(errno)); return false; } return true; }
/* * Attempt to determine if a PostgreSQL data directory is in use * by reading the pidfile. This is the same mechanism used by * "pg_ctl". * * This function will abort with appropriate log messages if a file error * is encountered, as the user will need to address the situation before * any further useful progress can be made. */ PgDirState is_pg_running(char *path) { long pid; FILE *pidf; char pid_file[MAXPGPATH]; /* it's reasonable to assume the pidfile name will not change */ snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", path); pidf = fopen(pid_file, "r"); if (pidf == NULL) { /* * No PID file - PostgreSQL shouldn't be running. From 9.3 (the * earliesty version we care about) removal of the PID file will * cause the postmaster to shut down, so it's highly unlikely * that PostgreSQL will still be running. */ if (errno == ENOENT) { return PG_DIR_NOT_RUNNING; } else { log_error(_("unable to open PostgreSQL PID file \"%s\""), pid_file); log_detail("%s", strerror(errno)); exit(ERR_BAD_CONFIG); } } /* * In the unlikely event we're unable to extract a PID from the PID file, * log a warning but assume we're not dealing with a running instance * as PostgreSQL should have shut itself down in these cases anyway. */ if (fscanf(pidf, "%ld", &pid) != 1) { /* Is the file empty? */ if (ftell(pidf) == 0 && feof(pidf)) { log_warning(_("PostgreSQL PID file \"%s\" is empty"), path); } else { log_warning(_("invalid data in PostgreSQL PID file \"%s\""), path); } return PG_DIR_NOT_RUNNING; } fclose(pidf); if (pid == getpid()) return PG_DIR_NOT_RUNNING; if (pid == getppid()) return PG_DIR_NOT_RUNNING; if (kill(pid, 0) == 0) return PG_DIR_RUNNING; return PG_DIR_NOT_RUNNING; }
static void process_message(const word describer, const char * const body, const size_t index) { char message_type[8]; char times[16]; time_t timestamp; char from[16], to[16], descr[16], wasf[16], wast[16]; jsmn_find_extract_token(body, tokens, index, "msg_type", message_type, sizeof(message_type)); _log(DEBUG, "Message name = \"%s\".", message_type); jsmn_find_extract_token(body, tokens, index, "time", times, sizeof(times)); times[10] = '\0'; timestamp = atol(times); time_t now = time(NULL); last_td_processed[describer] = now; if(((status_last_td_actual[describer] + 8) < timestamp) || ((status_last_td_processed + 8) < now)) { status_last_td_actual[describer] = timestamp; status_last_td_processed = now; char query[256]; sprintf(query, "update status SET last_td_processed = %ld", now); db_query(query); sprintf(query, "update td_status set last_timestamp = %ld WHERE d = '%s'", timestamp, describers[describer]); db_query(query); } if(!strcasecmp(message_type, "CA")) { jsmn_find_extract_token(body, tokens, index, "from", from, sizeof(from)); jsmn_find_extract_token(body, tokens, index, "to", to, sizeof(to)); jsmn_find_extract_token(body, tokens, index, "descr", descr, sizeof(descr)); strcpy(wasf, query_berth(describer, from)); strcpy(wast, query_berth(describer, to)); _log(DEBUG, "%s CA: Berth step (%s) Description \"%s\" from berth \"%s\" to berth \"%s\"", describers[describer], time_text(timestamp, true), descr, from, to); log_detail(timestamp, "%s CA: %s from %s to %s", describers[describer], descr, from, to); if(strcmp(wasf, descr) || strcmp(wast, "")) { _log(DEBUG, "Message %s CA: Step \"%s\" from \"%s\" to \"%s\" found \"%s\" in \"%s\" and \"%s\" in \"%s\".", describers[describer], descr, from, to, wasf, from, wast, to); } update_database(Berth, describer, from, ""); update_database(Berth, describer, to, descr); stats[CA]++; } else if(!strcasecmp(message_type, "CB")) { jsmn_find_extract_token(body, tokens, index, "from", from, sizeof(from)); jsmn_find_extract_token(body, tokens, index, "descr", descr, sizeof(descr)); strcpy(wasf, query_berth(describer, from)); _log(DEBUG, "%s CB: Berth cancel (%s) Description \"%s\" from berth \"%s\"", describers[describer], time_text(timestamp, true), descr, from); log_detail(timestamp, "%s CB: %s from %s", describers[describer], descr, from); if(strcmp(wasf, descr)) { _log(DEBUG, "Message %s CB: Cancel \"%s\" from \"%s\" found \"%s\" in \"%s\".", describers[describer], descr, from, wasf, from); } update_database(Berth, describer, from, ""); stats[CB]++; } else if(!strcasecmp(message_type, "CC")) { jsmn_find_extract_token(body, tokens, index, "to", to, sizeof(to)); jsmn_find_extract_token(body, tokens, index, "descr", descr, sizeof(descr)); _log(DEBUG, "%s CC: Berth interpose (%s) Description \"%s\" to berth \"%s\"", describers[describer], time_text(timestamp, true), descr, to); log_detail(timestamp, "%s CC: %s to %s", describers[describer], descr, to); update_database(Berth, describer, to, descr); stats[CC]++; } else if(!strcasecmp(message_type, "CT")) { // Heartbeat char report_time[16]; jsmn_find_extract_token(body, tokens, index, "report_time", report_time, sizeof(report_time)); _log(DEBUG, "%s CT: Heartbeat (%s) Report time = %s", describers[describer], time_text(timestamp, true), report_time); stats[CT]++; } else if(!strcasecmp(message_type, "SF")) { char address[16], data[32]; jsmn_find_extract_token(body, tokens, index, "address", address, sizeof(address)); jsmn_find_extract_token(body, tokens, index, "data", data, sizeof(data)); _log(DEBUG, "%s SF: Signalling update (%s) Address \"%s\", data \"%s\"", describers[describer], time_text(timestamp, true), address, data); word a = strtoul(address, NULL, 16); dword d = strtoul(data, NULL, 16); signalling_update("SF", describer, timestamp, a, d); stats[SF]++; } else if(!strcasecmp(message_type, "SG")) { char address[16], data[32]; jsmn_find_extract_token(body, tokens, index, "address", address, sizeof(address)); jsmn_find_extract_token(body, tokens, index, "data", data, sizeof(data)); _log(DEBUG, "%s SG: Signalling refresh (%s) Address \"%s\", data \"%s\"", describers[describer], time_text(timestamp, true), address, data); word a = strtoul(address, NULL, 16); dword d = strtoul(data, NULL, 16); _log(DEBUG, "a = %04x, d = %08x", a, d); signalling_update("SG", describer, timestamp, a , 0xff & (d >> 24)); signalling_update("SG", describer, timestamp, a + 1 , 0xff & (d >> 16)); signalling_update("SG", describer, timestamp, a + 2 , 0xff & (d >> 8 )); signalling_update("SG", describer, timestamp, a + 3 , 0xff & (d )); stats[SG]++; }
static bool slm_solve(void* context, real_t* B, real_t* x) { slm_t* mat = context; SuperMatrix* A = mat->A; // Copy B to the rhs vector. DNformat* rhs = mat->rhs.Store; double* Bi = rhs->nzval; for (int i = 0; i < mat->N; ++i) Bi[i] = (double)B[i]; if (mat->cperm == NULL) { mat->cperm = intMalloc(mat->N); mat->rperm = intMalloc(mat->N); } else if (mat->options.Fact != FACTORED) { // Jettison the existing factorization. Destroy_SuperNode_Matrix(&mat->L); Destroy_CompCol_Matrix(&mat->U); } // Do the solve. int info = 0, lwork = 0; void* work = NULL; double ferr, berr; GlobalLU_t glu; // "Global data structure" for SuperLU for helping with factorizations. double recip_pivot_growth = 1.0, rcond = 1.0; mem_usage_t mem_usage; if (mat->ilu_params == NULL) { // Factorize if necessary. if (mat->options.Fact == DOFACT) { int rhs_ncol = mat->rhs.ncol; mat->rhs.ncol = 0; polymec_suspend_fpe(); dgssvx(&mat->options, A, mat->cperm, mat->rperm, mat->etree, &mat->equil, mat->R, mat->C, &mat->L, &mat->U, work, lwork, &mat->rhs, &mat->X, &recip_pivot_growth, &rcond, &ferr, &berr, &glu, &mem_usage, &mat->stat, &info); polymec_restore_fpe(); mat->rhs.ncol = rhs_ncol; if ((info == 0) || (info == A->ncol+1)) { if (mat->equil != 'N') { if (mat->equil == 'R') log_debug("slm_solve: performed row equilibration."); else if (mat->equil == 'C') log_debug("slm_solve: performed column equilibration."); else if (mat->equil == 'B') log_debug("slm_solve: performed row/column equilibration."); } log_debug("slm_solve: L has %d nonzeros, U has %d.", ((SCformat*)mat->L.Store)->nnz, ((NCformat*)mat->U.Store)->nnz); #ifndef NDEBUG log_debug("slm_solve: recip pivot growth = %g, condition number = %g.", recip_pivot_growth, rcond); if (recip_pivot_growth < 0.01) { log_detail("slm_solve: WARNING: recip pivot growth for ILU factorization << 1."); log_detail("slm_solve: WARNING: Stability of LU factorization could be poor."); } #endif // Reuse the factorization. mat->options.Fact = FACTORED; } else log_debug("slm_solve: LU factorization failed."); } // Solve the factored system. if ((info == 0) || (info == A->ncol+1)) { polymec_suspend_fpe(); dgssvx(&mat->options, A, mat->cperm, mat->rperm, mat->etree, &mat->equil, mat->R, mat->C, &mat->L, &mat->U, work, lwork, &mat->rhs, &mat->X, &recip_pivot_growth, &rcond, &ferr, &berr, &glu, &mem_usage, &mat->stat, &info); polymec_restore_fpe(); } } else { // Incomplete LU factorization. // Factorize if necessary. if (mat->options.Fact == DOFACT) { int rhs_ncol = mat->rhs.ncol; mat->rhs.ncol = 0; polymec_suspend_fpe(); dgsisx(&mat->options, A, mat->cperm, mat->rperm, mat->etree, &mat->equil, mat->R, mat->C, &mat->L, &mat->U, NULL, 0, &mat->rhs, &mat->X, &recip_pivot_growth, &rcond, &glu, &mem_usage, &mat->stat, &info); polymec_restore_fpe(); mat->rhs.ncol = rhs_ncol; if ((info == 0) || (info == A->ncol+1)) { if (mat->equil != 'N') { if (mat->equil == 'R') log_debug("slm_solve: performed row equilibration."); else if (mat->equil == 'C') log_debug("slm_solve: performed column equilibration."); else if (mat->equil == 'B') log_debug("slm_solve: performed row/column equilibration."); } #ifndef NDEBUG log_debug("slm_solve: recip pivot growth = %g, condition number = %g.", recip_pivot_growth, rcond); if (recip_pivot_growth < 0.01) { log_detail("slm_solve: WARNING: recip pivot growth for ILU factorization << 1."); log_detail("slm_solve: WARNING: Stability of LU factorization could be poor."); } #endif // Reuse the factorization. mat->options.Fact = FACTORED; } else log_debug("slm_solve: incomplete LU factorization failed."); } // Solve the factored system. if ((info == 0) || (info == A->ncol+1)) { polymec_suspend_fpe(); dgsisx(&mat->options, A, mat->cperm, mat->rperm, mat->etree, &mat->equil, mat->R, mat->C, &mat->L, &mat->U, NULL, 0, &mat->rhs, &mat->X, &recip_pivot_growth, &rcond, &glu, &mem_usage, &mat->stat, &info); polymec_restore_fpe(); } } bool success = ((info == 0) || (info == A->ncol+1)); if (success) { // Copy the output vector to x. double* X = ((DNformat*)mat->X.Store)->nzval; for (int i = 0; i < mat->N; ++i) x[i] = (real_t)X[i]; } else { ASSERT(info > 0); if (mat->ilu_params == NULL) { log_debug("slm_solve: LU solve failed."); log_debug("slm_solve: (U is singular: U(%d, %d) = 0.)", info-1, info-1); } else { log_debug("slm_solve: ILU solve failed."); if (info < A->ncol) log_debug("slm_solve: (number of zero pivots in U = %d.)", info); else if (info == (A->ncol + 1)) log_debug("slm_solve: (U is nonsingular but rcond = %g.)", rcond); else log_debug("slm_solve: (Memory allocation failure.)"); } } return success; }