| 107 | } |
| 108 | |
| 109 | void csv_reader::load() |
| 110 | { |
| 111 | bool master = m_comm->am_world_master(); |
| 112 | setup_ifstreams(); |
| 113 | std::ifstream& ifs = *m_ifstreams[0]; |
| 114 | const El::mpi::Comm& world_comm = m_comm->get_world_comm(); |
| 115 | // Parse the header to determine how many columns there are. |
| 116 | // Skip rows if needed. |
| 117 | if (master) { |
| 118 | skip_rows(ifs, m_skip_rows); |
| 119 | } |
| 120 | m_comm->broadcast<int>(0, m_skip_rows, world_comm); |
| 121 | |
| 122 | // This will be broadcast from root to other procs, and will |
| 123 | // then be converted to std::vector<int> m_labels; this is because |
| 124 | // El::mpi::Broadcast<std::streampos> doesn't work |
| 125 | std::vector<long long> index; |
| 126 | |
| 127 | if (master) { |
| 128 | std::string line; |
| 129 | std::streampos header_start = ifs.tellg(); |
| 130 | // TODO: Skip comment lines. |
| 131 | if (std::getline(ifs, line)) { |
| 132 | m_num_cols = std::count(line.begin(), line.end(), m_separator) + 1; |
| 133 | if (m_skip_cols >= m_num_cols) { |
| 134 | throw lbann_exception( |
| 135 | "csv_reader: asked to skip more columns than are present"); |
| 136 | } |
| 137 | |
| 138 | if (!m_disable_labels) { |
| 139 | if (m_label_col < 0) { |
| 140 | // Last column becomes the label column. |
| 141 | m_label_col = m_num_cols - 1; |
| 142 | } |
| 143 | if (m_label_col >= m_num_cols) { |
| 144 | throw lbann_exception("csv_reader: label column" + |
| 145 | std::to_string(m_label_col) + |
| 146 | " is not present"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (!m_disable_responses) { |
| 151 | if (m_response_col < 0) { |
| 152 | // Last column becomes the response column. |
| 153 | m_response_col = m_num_cols - 1; |
| 154 | } |
| 155 | if (m_response_col >= m_num_cols) { |
| 156 | throw lbann_exception("csv_reader: response column" + |
| 157 | std::to_string(m_response_col) + |
| 158 | " is not present"); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | else { |
| 163 | throw lbann_exception("csv_reader: failed to read header in " + |
| 164 | get_data_filename()); |
| 165 | } |
| 166 | if (ifs.eof()) { |
no test coverage detected