コード例 #1
0
ファイル: Util.cs プロジェクト: backsapc/rs
        public void load_from_iterator(long _rows, long _cols, long _nnz, entry_iterator_t entry_it)
        {
            rows            = _rows;
            cols            = _cols;
            nnz             = _nnz;
            mem_alloc_by_me = true;
            with_weights    = entry_it.with_weights;
            val             = new double[nnz];
            val_t           = new double[nnz];

            row_idx = new long[nnz];
            col_idx = new long[nnz]; // switch to this for memory
            row_ptr = new long[rows + 1];
            col_ptr = new long[cols + 1];

            // a trick here to utilize the space the have been allocated
            long[]   perm        = new long[_nnz];
            long[]   tmp_row_idx = col_idx;
            long[]   tmp_col_idx = row_idx;
            double[] tmp_val     = val;

            for (long idx = 0; idx < _nnz; idx++)
            {
                rate_t rate = entry_it.next();
                row_ptr[rate.i + 1]++;
                col_ptr[rate.j + 1]++;
                tmp_row_idx[idx] = rate.i;
                tmp_col_idx[idx] = rate.j;
                tmp_val[idx]     = rate.v;
                perm[idx]        = idx;
            }

            // sort entries into row-majored ordering

            Array.Sort(perm, new SparseComp(tmp_row_idx, tmp_col_idx, true));

            // Generate CRS format
            for (long idx = 0; idx < _nnz; idx++)
            {
                val_t[idx]   = tmp_val[perm[idx]];
                col_idx[idx] = tmp_col_idx[perm[idx]];
            }

            // Calculate nnz for each row and col
            max_row_nnz = max_col_nnz = 0;
            for (long r = 1; r <= rows; ++r)
            {
                max_row_nnz = Math.Max(max_row_nnz, row_ptr[r]);
                row_ptr[r] += row_ptr[r - 1];
            }

            for (long c = 1; c <= cols; ++c)
            {
                max_col_nnz = Math.Max(max_col_nnz, col_ptr[c]);
                col_ptr[c] += col_ptr[c - 1];
            }

            // Transpose CRS into CCS matrix
            for (long r = 0; r < rows; ++r)
            {
                for (long i = row_ptr[r]; i < row_ptr[r + 1]; ++i)
                {
                    long c = col_idx[i];
                    row_idx[col_ptr[c]] = r;
                    val[col_ptr[c]]     = val_t[i];
                    if (with_weights)
                    {
                        weight[col_ptr[c]] = weight_t[i];
                    }
                    col_ptr[c]++;
                }
            }

            for (long c = cols; c > 0; --c)
            {
                col_ptr[c] = col_ptr[c - 1];
            }
            col_ptr[0] = 0;
        }
コード例 #2
0
ファイル: Util.cs プロジェクト: backsapc/rs
        public void load(long _rows, long _cols, long _nnz, string filename, bool with_weights = false)
        {
            entry_iterator_t entry_it = new entry_iterator_t(_nnz, filename, with_weights);

            load_from_iterator(_rows, _cols, _nnz, entry_it);
        }