subproblem.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef __SUBPROBLEM_H__
  2. #define __SUBPROBLEM_H__
  3. extern "C" {
  4. #include <event2/bufferevent.h>
  5. }
  6. #include <NTL/ZZ.h>
  7. #include <ostream>
  8. #include <string.h>
  9. NTL_CLIENT
  10. #define SUBPROBLEM_DESC_LEN (2 + (WORDS*3 + 3 + 1)*sizeof(unsigned int))
  11. struct Subproblem {
  12. unsigned short problemid;
  13. ZZ base;
  14. ZZ target;
  15. ZZ modulus;
  16. ZZ order;
  17. unsigned int dpfreq;
  18. unsigned char desc[1 + SUBPROBLEM_DESC_LEN];
  19. // Update the binary description of the subproblem. This is what
  20. // gets sent to the dpnodes and workers.
  21. void updatedesc(void)
  22. {
  23. desc[0] = 'P';
  24. memmove(desc+1, &problemid, 2);
  25. BytesFromZZ(desc+3, base, WORDS*sizeof(unsigned int));
  26. BytesFromZZ(desc+3+WORDS*sizeof(unsigned int), target,
  27. WORDS*sizeof(unsigned int));
  28. BytesFromZZ(desc+3+2*WORDS*sizeof(unsigned int), modulus,
  29. WORDS*sizeof(unsigned int));
  30. BytesFromZZ(desc+3+3*WORDS*sizeof(unsigned int), order,
  31. 3*sizeof(unsigned int));
  32. memmove(desc+3+(3*WORDS+3)*sizeof(unsigned int), &dpfreq,
  33. sizeof(unsigned int));
  34. }
  35. Subproblem(unsigned short id, const ZZ &b, const ZZ &t, const ZZ &m,
  36. const ZZ &o, unsigned int dpf) : problemid(id), base(b),
  37. target(t), modulus(m), order(o), dpfreq(dpf) {
  38. updatedesc();
  39. }
  40. // Initilize the Subproblem from the binary description, *without*
  41. // the leading 'P'
  42. Subproblem(const unsigned char *descnoP) {
  43. desc[0] = 'P';
  44. memmove(desc+1, descnoP, SUBPROBLEM_DESC_LEN);
  45. memmove(&problemid, desc+1, 2);
  46. ZZFromBytes(base, desc+3, WORDS*sizeof(unsigned int));
  47. ZZFromBytes(target, desc+3+WORDS*sizeof(unsigned int),
  48. WORDS*sizeof(unsigned int));
  49. ZZFromBytes(modulus, desc+3+2*WORDS*sizeof(unsigned int),
  50. WORDS*sizeof(unsigned int));
  51. ZZFromBytes(order, desc+3+3*WORDS*sizeof(unsigned int),
  52. 3*sizeof(unsigned int));
  53. memmove(&dpfreq, desc+3+(3*WORDS+3)*sizeof(unsigned int),
  54. sizeof(unsigned int));
  55. }
  56. // Dump for debug purposes
  57. void dump(ostream &os) const {
  58. os << "Subproblem " << problemid << "\n";
  59. os << "base = " << base << "\n";
  60. os << "target = " << target << "\n";
  61. os << "modulus = " << modulus << "\n";
  62. os << "order = " << order << "\n";
  63. os << "dpfreq = " << dpfreq << "\n";
  64. }
  65. // Write the subproblem to the given bufferevent
  66. void bev_write(struct bufferevent *bev) {
  67. bufferevent_write(bev, desc, 1 + SUBPROBLEM_DESC_LEN);
  68. }
  69. };
  70. #endif