File: conversion.h

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (802 lines) | stat: -rw-r--r-- 24,650 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
 *  Copyright 2008-2009 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*
 * Modifications to this file:
 * Copyright (c) 2014-2015, The University of Queensland
 * Licensed under the Apache License, Version 2.0.
 *
 */



#pragma once

#include <cusp/cmath.h>
#include <cusp/ell_matrix.h>
#include <cusp/exception.h>

#include <cusp/detail/host/conversion_utils.h>

#include <thrust/fill.h>
#include <thrust/extrema.h>
#include <thrust/count.h>

namespace cusp
{
namespace detail
{
namespace host
{

/////////////////////
// CDS Conversions //
/////////////////////
    
template <typename Matrix1, typename Matrix2>
void cds_to_csr(const Matrix1& src, Matrix2& dst)
{
    //typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    size_t num_entries = 0;
    size_t num_diagonals = src.diagonal_offsets.size();

    // count nonzero entries
    for (size_t i = 0; i < num_diagonals; i++)
    {
        num_entries += src.block_size*src.block_size*(src.num_rows-cusp::abs(src.diagonal_offsets[i])*src.block_size)/src.block_size;
    }
    
    dst.resize(src.num_rows, src.num_rows, num_entries);

    num_entries = 0;
    dst.row_offsets[0] = 0;

    // copy nonzero entries to CSR structure
    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(size_t n = 0; n < num_diagonals; n++)
        {
            if (src.diagonal_offsets[n] < 0 && i < cusp::abs(src.diagonal_offsets[n])*src.block_size)
                continue;
            if (src.diagonal_offsets[n] > 0 && i >= src.num_rows-src.diagonal_offsets[n]*src.block_size)
                continue;

            for (size_t j = 0; j < src.block_size; j++)
            {
                const ValueType value = src.values(i, n*src.block_size+j);

                dst.column_indices[num_entries] =
                    (src.diagonal_offsets[n]+i/src.block_size)*src.block_size+j;
                dst.values[num_entries] = value;
                num_entries++;
            }
        }

        dst.row_offsets[i + 1] = num_entries;
    }
}

template <typename Matrix1, typename Matrix2>
void cds_to_dia(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    const size_t num_diagonalblocks = src.diagonal_offsets.size();

    // number of diagonals for DIA format
    const size_t num_diagonals = num_diagonalblocks*(2*src.block_size-1);
    cusp::array1d<IndexType,cusp::host_memory> diagonal_offsets(num_diagonals, 0);

    size_t num_entries = 0;

    // count entries (with zero fill-in) for DIA format
    // and determine diagonal offsets
    for (size_t i = 0, n = 0; i < num_diagonalblocks; i++)
    {
        for (IndexType j = 1-(IndexType)src.block_size; j < (IndexType)src.block_size; j++)
        {
            const IndexType offset = src.diagonal_offsets[i] * src.block_size + j;
            diagonal_offsets[n++] = offset;
            num_entries += src.num_rows - cusp::abs(offset);
        }
    }

    // prepare destination matrix
    dst.resize(src.num_rows, src.num_rows, num_entries, num_diagonals);
    cusp::copy(diagonal_offsets, dst.diagonal_offsets);
    thrust::fill(dst.values.values.begin(), dst.values.values.end(), ValueType(0));

    for (size_t i = 0; i < num_diagonalblocks; i++)
    {
        const IndexType k = src.diagonal_offsets[i]*src.block_size;
        const IndexType first_row = std::max(0, -k);
        const IndexType num_rows = src.num_rows - cusp::abs(k);
        // row index is identical for CDS and DIA
        for (IndexType row = first_row; row < first_row+num_rows; row++)
        {
            for (IndexType j = 0; j < src.block_size; j++)
            {
                // index of "column" in DIA structure
                const IndexType col = i*(2*src.block_size-1)+(src.block_size-1-row%src.block_size)+j;
                dst.values(row,col) = src.values(row, i*src.block_size+j);
            }
        }
    }
}

/////////////////////
// COO Conversions //
/////////////////////
    
template <typename Matrix1, typename Matrix2>
void coo_to_csr(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    //typedef typename Matrix2::value_type ValueType;

    dst.resize(src.num_rows, src.num_cols, src.num_entries);
    
    // compute number of non-zero entries per row of A 
    thrust::fill(dst.row_offsets.begin(), dst.row_offsets.end(), IndexType(0));

    for (size_t n = 0; n < src.num_entries; n++)
        dst.row_offsets[src.row_indices[n]]++;

    // cumsum the num_entries per row to get dst.row_offsets[]
    IndexType cumsum = 0;
    for(size_t i = 0; i < src.num_rows; i++)
    {
        IndexType temp = dst.row_offsets[i];
        dst.row_offsets[i] = cumsum;
        cumsum += temp;
    }
    dst.row_offsets[src.num_rows] = cumsum;

    // write Aj,Ax into dst.column_indices,dst.values
    for(size_t n = 0; n < src.num_entries; n++)
    {
        IndexType row  = src.row_indices[n];
        IndexType dest = dst.row_offsets[row];

        dst.column_indices[dest] = src.column_indices[n];
        dst.values[dest]         = src.values[n];

        dst.row_offsets[row]++;
    }

    IndexType last = 0; 
    for(size_t i = 0; i <= src.num_rows; i++)
    {
        IndexType temp = dst.row_offsets[i];
        dst.row_offsets[i]  = last;
        last   = temp;
    }

    //csr may contain duplicates
}

template <typename Matrix1, typename Matrix2>
void coo_to_array2d(const Matrix1& src, Matrix2& dst)
{
    //typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    dst.resize(src.num_rows, src.num_cols);

    thrust::fill(dst.values.begin(), dst.values.end(), ValueType(0));

    for(size_t n = 0; n < src.num_entries; n++)
        dst(src.row_indices[n], src.column_indices[n]) += src.values[n]; //sum duplicates
}

/////////////////////
// CSR Conversions //
/////////////////////

template <typename Matrix1, typename Matrix2>
void csr_to_cds(const Matrix1& src, Matrix2& dst,
                const size_t alignment = 32)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    // compute number of occupied diagonals and enumerate them
    size_t num_diagonals = 0;

    // block size is always 1 for this conversion
    size_t block_size = 1;

    cusp::array1d<IndexType,cusp::host_memory> diag_map(src.num_rows + src.num_cols, 0);

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i+1]; jj++)
        {
            size_t j         = src.column_indices[jj];
            size_t map_index = (src.num_rows - i) + j; //offset shifted by + num_rows

            if(diag_map[map_index] == 0)
            {
                diag_map[map_index] = 1;
                num_diagonals++;
            }
        }
    }
   

    // allocate CDS structure
    dst.resize(src.num_rows, src.num_entries, num_diagonals, block_size, alignment);

    // fill in diagonal_offsets array
    for(size_t n = 0, diag = 0; n < src.num_rows + src.num_cols; n++)
    {
        if(diag_map[n] == 1)
        {
            diag_map[n] = diag;
            dst.diagonal_offsets[diag] = (IndexType) n - (IndexType) src.num_rows;
            diag++;
        }
    }

    // fill in values array
    thrust::fill(dst.values.values.begin(), dst.values.values.end(), ValueType(0));

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i+1]; jj++)
        {
            size_t j = src.column_indices[jj];
            size_t map_index = (src.num_rows - i) + j; //offset shifted by + num_rows
            size_t diag = diag_map[map_index];
        
            dst.values(i, diag) = src.values[jj];
        }
    }
}

template <typename Matrix1, typename Matrix2>
void csr_to_coo(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    //typedef typename Matrix2::value_type ValueType;

    dst.resize(src.num_rows, src.num_cols, src.num_entries);
   
    // TODO replace with offsets_to_indices
    for(size_t i = 0; i < src.num_rows; i++)
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i + 1]; jj++)
            dst.row_indices[jj] = i;

    cusp::copy(src.column_indices, dst.column_indices);
    cusp::copy(src.values,         dst.values);
}

template <typename Matrix1, typename Matrix2>
void csr_to_dia(const Matrix1& src, Matrix2& dst,
                const size_t alignment = 32)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    // compute number of occupied diagonals and enumerate them
    size_t num_diagonals = 0;

    cusp::array1d<IndexType,cusp::host_memory> diag_map(src.num_rows + src.num_cols, 0);

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i+1]; jj++)
        {
            size_t j         = src.column_indices[jj];
            size_t map_index = (src.num_rows - i) + j; //offset shifted by + num_rows

            if(diag_map[map_index] == 0)
            {
                diag_map[map_index] = 1;
                num_diagonals++;
            }
        }
    }
   

    // allocate DIA structure
    dst.resize(src.num_rows, src.num_cols, src.num_entries, num_diagonals, alignment);

    // fill in diagonal_offsets array
    for(size_t n = 0, diag = 0; n < src.num_rows + src.num_cols; n++)
    {
        if(diag_map[n] == 1)
        {
            diag_map[n] = diag;
            dst.diagonal_offsets[diag] = (IndexType) n - (IndexType) src.num_rows;
            diag++;
        }
    }

    // fill in values array
    thrust::fill(dst.values.values.begin(), dst.values.values.end(), ValueType(0));

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i+1]; jj++)
        {
            size_t j = src.column_indices[jj];
            size_t map_index = (src.num_rows - i) + j; //offset shifted by + num_rows
            size_t diag = diag_map[map_index];
        
            dst.values(i, diag) = src.values[jj];
        }
    }
}

template <typename Matrix1, typename Matrix2>
void csr_to_hyb(const Matrix1& src, Matrix2& dst,
                const size_t num_entries_per_row,
                const size_t alignment = 32)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    // The ELL portion of the HYB matrix will have 'num_entries_per_row' columns.
    // Nonzero values that do not fit within the ELL structure are placed in the 
    // COO format portion of the HYB matrix.
    
    // compute number of nonzeros in the ELL and COO portions
    size_t num_ell_entries = 0;
    for(size_t i = 0; i < src.num_rows; i++)
        num_ell_entries += thrust::min<size_t>(num_entries_per_row, src.row_offsets[i+1] - src.row_offsets[i]); 

    IndexType num_coo_entries = src.num_entries - num_ell_entries;

    dst.resize(src.num_rows, src.num_cols, 
               num_ell_entries, num_coo_entries, 
               num_entries_per_row, alignment);

    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;

    // pad out ELL format with zeros
    thrust::fill(dst.ell.column_indices.values.begin(), dst.ell.column_indices.values.end(), invalid_index);
    thrust::fill(dst.ell.values.values.begin(),         dst.ell.values.values.end(),         ValueType(0));

    for(size_t i = 0, coo_nnz = 0; i < src.num_rows; i++)
    {
        size_t n = 0;
        IndexType jj = src.row_offsets[i];

        // copy up to num_cols_per_row values of row i into the ELL
        while(jj < src.row_offsets[i+1] && n < num_entries_per_row)
        {
            dst.ell.column_indices(i,n) = src.column_indices[jj];
            dst.ell.values(i,n)         = src.values[jj];
            jj++, n++;
        }

        // copy any remaining values in row i into the COO
        while(jj < src.row_offsets[i+1])
        {
            dst.coo.row_indices[coo_nnz]    = i;
            dst.coo.column_indices[coo_nnz] = src.column_indices[jj];
            dst.coo.values[coo_nnz]         = src.values[jj];
            jj++; coo_nnz++;
        }
    }
}


template <typename Matrix1, typename Matrix2>
void csr_to_ell(const Matrix1& src, Matrix2& dst,
                const size_t num_entries_per_row, const size_t alignment = 32)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    // compute number of nonzeros
    size_t num_entries = 0;
#pragma omp parallel for reduction( +: num_entries)
    for(size_t i = 0; i < src.num_rows; i++)
        num_entries += thrust::min<size_t>(num_entries_per_row, src.row_offsets[i+1] - src.row_offsets[i]); 

    dst.resize(src.num_rows, src.num_cols, num_entries, num_entries_per_row, alignment);

    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;

    // pad out ELL format with zeros
    thrust::fill(dst.column_indices.values.begin(), dst.column_indices.values.end(), invalid_index);
    thrust::fill(dst.values.values.begin(),         dst.values.values.end(),         ValueType(0));

#pragma omp parallel for
    for(size_t i = 0; i < src.num_rows; i++)
    {
        size_t n = 0;
        IndexType jj = src.row_offsets[i];

        // copy up to num_cols_per_row values of row i into the ELL
        while(jj < src.row_offsets[i+1] && n < num_entries_per_row)
        {
            dst.column_indices(i,n) = src.column_indices[jj];
            dst.values(i,n)         = src.values[jj];
            jj++, n++;
        }
    }
}

    
template <typename Matrix1, typename Matrix2>
void csr_to_array2d(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;

    dst.resize(src.num_rows, src.num_cols);

    thrust::fill(dst.values.begin(), dst.values.end(), ValueType(0));

    for(size_t i = 0; i < src.num_rows; i++)
        for(IndexType jj = src.row_offsets[i]; jj < src.row_offsets[i+1]; jj++)
            dst(i, src.column_indices[jj]) += src.values[jj]; //sum duplicates
}


/////////////////////
// DIA Conversions //
/////////////////////

template <typename Matrix1, typename Matrix2>
void dia_to_cds(const Matrix1& src, Matrix2& dst)
{
    //typedef typename Matrix2::index_type IndexType;
    //typedef typename Matrix2::value_type ValueType;

    // allocate CDS structure
    dst.resize(src.num_rows, src.num_entries, src.diagonal_offsets.size(), 1, src.values.pitch);

    cusp::copy(src.diagonal_offsets, dst.diagonal_offsets);
    cusp::copy(src.values,           dst.values);
}

template <typename Matrix1, typename Matrix2>
void dia_to_csr(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    size_t num_entries = 0;
    size_t num_diagonals = src.diagonal_offsets.size();
    
    // count nonzero entries
    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(size_t n = 0; n < num_diagonals; n++)
        {
            const IndexType j = i + src.diagonal_offsets[n];

            if(j >= 0 && static_cast<size_t>(j) < src.num_cols && src.values(i,n) != ValueType(0))
                num_entries++;
        }
    }

    dst.resize(src.num_rows, src.num_cols, num_entries);

    num_entries = 0;
    dst.row_offsets[0] = 0;

    // copy nonzero entries to CSR structure
    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(size_t n = 0; n < num_diagonals; n++)
        {
            const IndexType j = i + src.diagonal_offsets[n];

            if(j >= 0 && static_cast<size_t>(j) < src.num_cols)
            {
                const ValueType value = src.values(i, n);

                if (value != ValueType(0))
                {
                    dst.column_indices[num_entries] = j;
                    dst.values[num_entries] = value;
                    num_entries++;
                }
            }
        }

        dst.row_offsets[i + 1] = num_entries;
    }
}

/////////////////////
// ELL Conversions //
/////////////////////

template <typename Matrix1, typename Matrix2>
void ell_to_coo(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;
    
    dst.resize(src.num_rows, src.num_cols, src.num_entries);

    size_t num_entries = 0;

    const size_t num_entries_per_row = src.column_indices.num_cols;

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(size_t n = 0; n < num_entries_per_row; n++)
        {
            const IndexType j = src.column_indices(i,n);
            const ValueType v = src.values(i,n);

            if(j != invalid_index)
            {
                dst.row_indices[num_entries]    = i;
                dst.column_indices[num_entries] = j;
                dst.values[num_entries]         = v;
                num_entries++;
            }
        }
    }
}

template <typename Matrix1, typename Matrix2>
void ell_to_csr(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;

    dst.resize(src.num_rows, src.num_cols, src.num_entries);

    size_t num_entries = 0;
    dst.row_offsets[0] = 0;

    const size_t num_entries_per_row = src.column_indices.num_cols;

    for(size_t i = 0; i < src.num_rows; i++)
    {
        for(size_t n = 0; n < num_entries_per_row; n++)
        {
            const IndexType j = src.column_indices(i,n);
            const ValueType v = src.values(i,n);

            if(j != invalid_index)
            {
                dst.column_indices[num_entries] = j;
                dst.values[num_entries]         = v;
                num_entries++;
            }
        }

        dst.row_offsets[i + 1] = num_entries;
    }
}

/////////////////////
// HYB Conversions //
/////////////////////

template <typename Matrix1, typename Matrix2>
void hyb_to_coo(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    dst.resize(src.num_rows, src.num_cols, src.num_entries);

    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;

    const size_t num_entries_per_row = src.ell.column_indices.num_cols;

    size_t num_entries  = 0;
    size_t coo_progress = 0;
    
    // merge each row of the ELL and COO parts into a single COO row
    for(size_t i = 0; i < src.num_rows; i++)
    {
        // append the i-th row from the ELL part
        for(size_t n = 0; n < num_entries_per_row; n++)
        {
            const IndexType j = src.ell.column_indices(i,n);
            const ValueType v = src.ell.values(i,n);

            if(j != invalid_index)
            {
                dst.row_indices[num_entries]    = i;
                dst.column_indices[num_entries] = j;
                dst.values[num_entries]         = v;
                num_entries++;
            }
        }

        // append the i-th row from the COO part
        while (coo_progress < src.coo.num_entries && static_cast<size_t>(src.coo.row_indices[coo_progress]) == i)
        {
            dst.row_indices[num_entries]    = i;
            dst.column_indices[num_entries] = src.coo.column_indices[coo_progress];
            dst.values[num_entries]         = src.coo.values[coo_progress];
            num_entries++;
            coo_progress++;
        }
    }
}

template <typename Matrix1, typename Matrix2>
void hyb_to_csr(const Matrix1& src, Matrix2& dst)
{
    typedef typename Matrix2::index_type IndexType;
    typedef typename Matrix2::value_type ValueType;
    
    dst.resize(src.num_rows, src.num_cols, src.num_entries);

    const IndexType invalid_index = cusp::ell_matrix<IndexType, ValueType, cusp::host_memory>::invalid_index;

    const size_t num_entries_per_row = src.ell.column_indices.num_cols;

    size_t num_entries = 0;
    dst.row_offsets[0] = 0;
    
    size_t coo_progress = 0;
    
    // merge each row of the ELL and COO parts into a single CSR row
    for(size_t i = 0; i < src.num_rows; i++)
    {
        // append the i-th row from the ELL part
        for(size_t n = 0; n < num_entries_per_row; n++)
        {
            const IndexType j = src.ell.column_indices(i,n);
            const ValueType v = src.ell.values(i,n);

            if(j != invalid_index)
            {
                dst.column_indices[num_entries] = j;
                dst.values[num_entries]         = v;
                num_entries++;
            }
        }

        // append the i-th row from the COO part
        while (coo_progress < src.coo.num_entries && static_cast<size_t>(src.coo.row_indices[coo_progress]) == i)
        {
            dst.column_indices[num_entries] = src.coo.column_indices[coo_progress];
            dst.values[num_entries]         = src.coo.values[coo_progress];
            num_entries++;
            coo_progress++;
        }

        dst.row_offsets[i + 1] = num_entries;
    }
}


/////////////////////////
// Array1d Conversions //
/////////////////////////
template <typename Matrix1, typename Matrix2>
void array2d_to_array1d(const Matrix1& src, Matrix2& dst)
{
  if (src.num_rows == 0 && src.num_cols == 0)
  {
    dst.resize(0);
  }
  else if (src.num_cols == 1)
  {
    dst.resize(src.num_rows);

    for (size_t i = 0; i < src.num_rows; i++)
      dst[i] = src(i,0);
  }
  else if (src.num_rows == 1)
  {
    dst.resize(src.num_cols);

    for (size_t j = 0; j < src.num_cols; j++)
      dst[j] = src(0,j);
  }
  else
  {
    throw cusp::format_conversion_exception("array2d to array1d conversion is only defined for row or column vectors");
  }
}

/////////////////////////
// Array2d Conversions //
/////////////////////////
template <typename Matrix1, typename Matrix2>
void array1d_to_array2d(const Matrix1& src, Matrix2& dst)
{
  dst.resize(src.size(),1);

  for (size_t i = 0; i < src.size(); i++)
    dst(i,0) = src[i];
}

template <typename Matrix1, typename Matrix2>
void array2d_to_coo(const Matrix1& src, Matrix2& dst)
{
  //typedef typename Matrix2::index_type IndexType;
  typedef typename Matrix2::value_type ValueType;

  // count number of nonzero entries in array
  size_t nnz = 0;
  
  for(size_t i = 0; i < src.num_rows; i++)
  {
    for(size_t j = 0; j < src.num_cols; j++)
    {
      if (src(i,j) != ValueType(0))
        nnz++;
    }
  }

  dst.resize(src.num_rows, src.num_cols, nnz);

  nnz = 0;

  for(size_t i = 0; i < src.num_rows; i++)
  {
    for(size_t j = 0; j < src.num_cols; j++)
    {
      if (src(i,j) != ValueType(0))
      {
        dst.row_indices[nnz]    = i;
        dst.column_indices[nnz] = j;
        dst.values[nnz]         = src(i,j);
        nnz++;
      }
    }
  }
}

template <typename Matrix1, typename Matrix2>
void array2d_to_csr(const Matrix1& src, Matrix2& dst)
{
  typedef typename Matrix2::index_type IndexType;
  typedef typename Matrix2::value_type ValueType;
  
  IndexType nnz = src.num_entries - thrust::count(src.values.begin(), src.values.end(), ValueType(0));

  dst.resize(src.num_rows, src.num_cols, nnz);

  IndexType num_entries = 0;

  for(size_t i = 0; i < src.num_rows; i++)
  {
    dst.row_offsets[i] = num_entries;

    for(size_t j = 0; j < src.num_cols; j++)
    {
      if (src(i,j) != ValueType(0))
      {
        dst.column_indices[num_entries] = j;
        dst.values[num_entries]         = src(i,j);
        num_entries++;
      }
    }
  }

  dst.row_offsets[src.num_rows] = num_entries;
}

} // end namespace host
} // end namespace detail
} // end namespace cusp