Example #1
0
 public LongMatrix1D vectorize() {
   final DenseLongMatrix1D v = new DenseLongMatrix1D((int) size());
   int nthreads = ConcurrencyUtils.getNumberOfThreads();
   if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
     nthreads = Math.min(nthreads, columns);
     Future<?>[] futures = new Future[nthreads];
     int k = columns / nthreads;
     for (int j = 0; j < nthreads; j++) {
       final int firstCol = j * k;
       final int lastCol = (j == nthreads - 1) ? columns : firstCol + k;
       final int firstidx = j * k * rows;
       futures[j] =
           ConcurrencyUtils.submit(
               new Runnable() {
                 public void run() {
                   int idx = firstidx;
                   for (int c = firstCol; c < lastCol; c++) {
                     for (int r = 0; r < rows; r++) {
                       v.setQuick(idx++, getQuick(r, c));
                     }
                   }
                 }
               });
     }
     ConcurrencyUtils.waitForCompletion(futures);
   } else {
     int idx = 0;
     for (int c = 0; c < columns; c++) {
       for (int r = 0; r < rows; r++) {
         v.setQuick(idx++, getQuick(r, c));
       }
     }
   }
   return v;
 }
Example #2
0
  public LongMatrix2D assign(final long[] values) {
    if (content instanceof DiagonalLongMatrix2D) {
      int dlength = ((DiagonalLongMatrix2D) content).dlength;
      final long[] elems = ((DiagonalLongMatrix2D) content).elements;
      if (values.length != dlength)
        throw new IllegalArgumentException(
            "Must have same length: length=" + values.length + " dlength=" + dlength);
      int nthreads = ConcurrencyUtils.getNumberOfThreads();
      if ((nthreads > 1) && (dlength >= ConcurrencyUtils.getThreadsBeginN_2D())) {
        nthreads = Math.min(nthreads, dlength);
        Future<?>[] futures = new Future[nthreads];
        int k = dlength / nthreads;
        for (int j = 0; j < nthreads; j++) {
          final int firstIdx = j * k;
          final int lastIdx = (j == nthreads - 1) ? dlength : firstIdx + k;
          futures[j] =
              ConcurrencyUtils.submit(
                  new Runnable() {

                    public void run() {
                      for (int i = firstIdx; i < lastIdx; i++) {
                        elems[i] = values[i];
                      }
                    }
                  });
        }
        ConcurrencyUtils.waitForCompletion(futures);
      } else {
        for (int i = 0; i < dlength; i++) {
          elems[i] = values[i];
        }
      }
      return this;
    } else {
      return super.assign(values);
    }
  }