1 /*-------------------------------------------------------------------------
5 * Copyright (c) 1994, Regents of the University of California
9 * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.22 1999/07/16 05:00:05 momjian Exp $
11 *-------------------------------------------------------------------------
14 #include <sys/types.h>
19 #include "regex/utils.h"
24 #include "catalog/pg_type.h"
26 #include "libpq/be-fsstubs.h"
27 #include "libpq/libpq-fs.h"
28 #include "optimizer/internal.h"
29 #include "utils/array.h"
30 #include "utils/memutils.h"
32 #define INFTY 500000000
35 #define quot_ceil(x,y) (((x)+(y)-1)/(y))
36 #define min(x,y) (((x) < (y))? (x) : (y))
37 #define max(x,y) (((x) > (y))? (x) : (y))
39 static CHUNK_INFO cInfo;
41 /* non-export function prototypes */
42 static int _FindBestChunk(int size, int *dmax, int *dbest, int dim,
43 int A[MAXPAT][MAXDIM + 1], int N);
44 static int get_next(int *d, int k, int C, int *dmax);
45 static void initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk);
48 static void _ConvertToChunkFile(int n, int baseSize, int *dim, int *C,
49 int srcfd, int destfd);
50 static void read_chunk(int *chunk_no, int *C, char *a_chunk, int srcfd,
51 int n, int baseSize, int *PX, int *dist);
52 static int write_chunk(struct varlena * a_chunk, int ofile);
53 static int seek_and_read(int pos, int size, char *buff, int fp, int from);
56 static int GetChunkSize(FILE *fd, int ndim, int dim[MAXDIM], int baseSize,
59 /*------------------------------------------------------------------------
61 * converts an input array to chunked format using the information
62 * provided by the access pattern.
64 * creates a new file that stores the chunked array and returns
65 * information about the chunked file
66 *-----------------------------------------------------------------------
85 if (chunkfile == NULL)
92 /* create new LO for chunked file */
93 chunkfile = _array_newLO(&cfd, fileFlag);
95 cfd = LOopen(chunkfile, O_RDONLY);
97 elog(ERROR, "Unable to open chunk file");
100 strcpy(cInfo.lo_name, chunkfile);
102 /* find chunk size */
103 csize = GetChunkSize(afd, ndim, dim, baseSize, chunk);
107 /* copy data from input file to chunked file */
108 _ConvertToChunkFile(ndim, baseSize, dim, chunk, fd, cfd);
111 initialize_info(&cInfo, ndim, dim, chunk);
112 *nbytes = sizeof(CHUNK_INFO);
113 return (char *) &cInfo;
116 /*--------------------------------------------------------------------------
118 * given an access pattern and array dimensionality etc, this program
119 * returns the dimensions of the chunk in "d"
120 *-----------------------------------------------------------------------
123 GetChunkSize(FILE *fd,
133 int A[MAXPAT][MAXDIM + 1],
137 * ----------- read input ------------
139 fscanf(fd, "%d", &N);
141 elog(ERROR, "array_in: too many access pattern elements");
142 for (i = 0; i < N; i++)
143 for (j = 0; j < ndim + 1; j++)
144 if (fscanf(fd, "%d ", &(A[i][j])) == EOF)
145 elog(ERROR, "array_in: bad access pattern input");
148 * estimate chunk size
150 for (i = 0; i < ndim; i++)
151 for (j = 0, dmax[i] = 1; j < N; j++)
152 if (dmax[i] < A[j][i])
154 csize = BLCKSZ / baseSize;
156 _FindBestChunk(csize, dmax, d, ndim, A, N);
161 /*-------------------------------------------------------------------------
163 * This routine does most of the number crunching to compute the
164 * optimal chunk shape.
165 * Called by GetChunkSize
166 *------------------------------------------------------------------------
169 _FindBestChunk(int size,
173 int A[MAXPAT][MAXDIM + 1],
182 while (get_next(d, dim, size, dmax))
186 * compute the number of page fetches for a given chunk size (*d)
187 * and access pattern (**A)
193 for (i = 0, tc = 0; i < N; i++)
195 for (j = 0, nc = 1; j < dim; j++)
196 nc *= quot_ceil(A[i][j], d[j]);
202 * tc holds the total number of page fetches
207 for (j = 0; j < dim; dbest[j] = d[j], j++)
214 /*----------------------------------------------------------------------
216 * Called by _GetBestChunk to get the next tuple in the lexicographic order
217 *---------------------------------------------------------------------
220 get_next(int *d, int k, int C, int *dmax)
229 for (j = k - 1; j >= 0; j--)
231 d[j] = min(temp, dmax[j]);
232 temp = max(1, temp / d[j]);
237 for (j = 0, temp = 1; j < k; j++)
240 for (i = k - 1; i >= 0; i--)
243 if (((temp * (d[i] + 1)) < C) && (d[i] + 1 <= dmax[i]))
251 d[i] = min(dmax[i], j / (j / d[i]));
255 for (j = k - 1; j > i; j--)
257 d[j] = min(temp, dmax[j]);
258 temp = max(1, temp / d[j]);
264 static char a_chunk[BLCKSZ + VARHDRSZ]; /* VARHDRSZ since a_chunk is in
270 initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk)
274 for (i = 0; i < ndim; i++)
278 /*--------------------------------------------------------------------------
279 * Procedure reorganize_data():
280 * This procedure reads the input multidimensional array that is organised
281 * in the order specified by array "X" and breaks it up into chunks of
282 * dimensions specified in "C".
284 * This is a very slow process, since reading and writing of LARGE files
287 *-------------------------------------------------------------------------
291 _ConvertToChunkFile(int n,
298 int max_chunks[MAXDIM],
306 for (i = 0; i < n; chunk_no[i++] = 0)
308 max_chunks[i] = dim[i] / C[i];
312 temp = csize + VARHDRSZ;
313 memmove(a_chunk, &temp, VARHDRSZ);
315 mda_get_prod(n, dim, PX);
316 mda_get_offset_values(n, dist, PX, C);
317 for (i = 0; i < n; dist[i] *= baseSize, i++)
321 read_chunk(chunk_no, C, &(a_chunk[VARHDRSZ]), srcfd, n, baseSize, PX, dist);
322 write_chunk((struct varlena *) a_chunk, destfd);
323 } while (next_tuple(n, chunk_no, max_chunks) != -1);
326 /*--------------------------------------------------------------------------
328 * reads a chunk from the input files into a_chunk, the position of the
329 * chunk is specified by chunk_no
330 *--------------------------------------------------------------------------
333 read_chunk(int *chunk_no,
351 for (i = start_pos = 0; i < n; i++)
353 pos[i] = chunk_no[i] * C[i];
354 start_pos += pos[i] * PX[i];
356 start_pos *= baseSize;
358 /* Read a block of dimesion C starting at co-ordinates pos */
359 unit_transfer = C[n - 1] * baseSize;
361 for (i = 0; i < n; indx[i++] = 0)
364 seek_and_read(fpOff, unit_transfer, a_chunk, srcfd, SEEK_SET);
365 fpOff += unit_transfer;
368 while ((j = next_tuple(n - 1, indx, C)) != -1)
371 seek_and_read(fpOff, unit_transfer, &(a_chunk[cp]), srcfd, SEEK_SET);
373 fpOff += unit_transfer;
377 /*--------------------------------------------------------------------------
379 * writes a chunk of size csize into the output file
380 *--------------------------------------------------------------------------
383 write_chunk(struct varlena * a_chunk, int ofile)
388 got_n = LOwrite(ofile, a_chunk);
393 /*--------------------------------------------------------------------------
395 * seeks to the asked location in the input file and reads the
396 * appropriate number of blocks
397 * Called By: read_chunk()
398 *--------------------------------------------------------------------------
401 seek_and_read(int pos, int size, char *buff, int fp, int from)
403 struct varlena *v = NULL;
405 /* Assuming only one file */
406 if (lo_lseek(fp, pos, from) < 0)
407 elog(ERROR, "File seek error");
409 v = (struct varlena *) LOread(fp, size);
411 if (VARSIZE(v) - VARHDRSZ < size)
412 elog(ERROR, "File read error");
413 memmove(buff, VARDATA(v), size);
421 /*----------------------------------------------------------------------------
423 * returns the subarray specified bu the range indices "st" and "endp"
424 * from the chunked array stored in file "fp"
425 *---------------------------------------------------------------------------
428 _ReadChunkArray(int *st,
443 int chunk_span[MAXDIM],
445 int chunk_st[MAXDIM],
454 int range_st[MAXDIM],
467 int srcOff; /* Needed since LO don't understand
469 char *baseDestFp = (char *) destfp;
471 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
474 dim = ARR_DIMS(array);
475 lb = ARR_LBOUND(array);
480 temp = dim[n - 1] / C[n - 1];
481 for (i = n - 2; i >= 0; i--)
483 PC[i] = PC[i + 1] * temp;
484 temp = dim[i] / C[i];
488 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++)
490 mda_get_prod(n, C, PCHUNK);
491 mda_get_range(n, array_span, st, endp);
492 mda_get_prod(n, array_span, PA);
494 array2chunk_coord(n, C, st, chunk_st);
495 array2chunk_coord(n, C, endp, chunk_end);
496 mda_get_range(n, chunk_span, chunk_st, chunk_end);
497 mda_get_offset_values(n, dist, PC, chunk_span);
499 for (i = 0; i < n; i++)
502 range_end[i] = min(chunk_st[i] * C[i] + C[i] - 1, endp[i]);
505 for (i = j = 0; i < n; i++)
506 j += chunk_st[i] * PC[i];
507 temp_seek = srcOff = j * csize * bsize;
508 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
512 for (i = 0; i < n; chunk_off[i++] = 0)
518 /* Write chunk (chunk_st) to output buffer */
519 mda_get_range(n, array_span, range_st, range_end);
520 mda_get_offset_values(n, adist, PA, array_span);
521 mda_get_offset_values(n, cdist, PCHUNK, array_span);
522 for (i = 0; i < n; range[i] = range_st[i] - st[i], i++);
523 bptr = tuple2linear(n, range, PA);
524 for (i = 0; i < n; range[i++] = 0);
529 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
533 destfp = baseDestFp + bptr;
534 for (i = 0, block_seek = 0; i < n; i++)
535 block_seek += (range_st[i] - (chunk_st[i] + chunk_off[i])
537 if (dist[jj] + block_seek + temp_seek)
539 temp = (dist[jj] * csize + block_seek + temp_seek) * bsize;
541 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
544 for (i = n - 1, to_read = bsize; i >= 0;
545 to_read *= min(C[i], array_span[i]), i--)
546 if (cdist[i] || adist[i])
552 srcOff += (cdist[j] * bsize);
553 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
556 block_seek += cdist[j];
557 bptr += adist[j] * bsize;
560 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
564 destfp = baseDestFp + bptr;
565 temp = _LOtransfer((char **) &destfp, to_read, 1, (char **) &fp, 1, isDestLO);
569 words_read += to_read;
571 block_seek += (to_read / bsize);
574 * compute next tuple in *range
583 range[i] = (range[i] + 1) % array_span[i];
584 for (x = i; x * (!range[x]); x--)
585 range[x - 1] = (range[x - 1] + 1) % array_span[x - 1];
599 * end of compute next tuple -- j is set to -1 if tuple
604 block_seek = csize - block_seek;
605 temp_seek = block_seek;
606 jj = next_tuple(n, chunk_off, chunk_span);
609 range_st[jj] = (chunk_st[jj] + chunk_off[jj]) * C[jj];
610 range_end[jj] = min(range_st[jj] + C[jj] - 1, endp[jj]);
612 for (i = jj + 1; i < n; i++)
615 range_end[i] = min((chunk_st[i] + chunk_off[i]) * C[i] + C[i] - 1, endp[i]);
621 /*------------------------------------------------------------------------
623 * returns one element of the chunked array as specified by the index "st"
624 * the chunked file descriptor is "fp"
625 *-------------------------------------------------------------------------
628 _ReadChunkArray1El(int *st,
639 int chunk_st[MAXDIM];
648 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
651 lb = ARR_LBOUND(array);
653 dim = ARR_DIMS(array);
657 temp = dim[n - 1] / C[n - 1];
658 for (i = n - 2; i >= 0; i--)
660 PC[i] = PC[i + 1] * temp;
661 temp = dim[i] / C[i];
665 for (i = 0; i < n; st[i] -= lb[i], i++);
666 mda_get_prod(n, C, PCHUNK);
668 array2chunk_coord(n, C, st, chunk_st);
670 for (i = j = 0; i < n; i++)
671 j += chunk_st[i] * PC[i];
674 for (i = 0; i < n; i++)
675 srcOff += (st[i] - chunk_st[i] * C[i]) * PCHUNK[i];
678 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
681 return (struct varlena *) LOread(fp, bsize);
683 return (struct varlena *) 0;