dynclock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 nelisky
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include "dynclock.h"
  12. #include "miner.h"
  13. void dclk_prepare(struct dclk_data *data)
  14. {
  15. *data = (struct dclk_data){
  16. // after 275 sample periods
  17. .minGoodSamples = 150.,
  18. .freqMinM = 1,
  19. };
  20. }
  21. void dclk_msg_freqchange(const char *repr, int oldFreq, int newFreq, const char *tail)
  22. {
  23. applog(LOG_NOTICE, "%"PRIpreprv": Frequency %s from %u to %u MHz%s",
  24. repr,
  25. (oldFreq > newFreq ? "dropped" : "raised "),
  26. oldFreq, newFreq,
  27. tail ?: ""
  28. );
  29. }
  30. bool dclk_updateFreq(struct dclk_data *data, dclk_change_clock_func_t changeclock, struct thr_info *thr)
  31. {
  32. struct cgpu_info *cgpu = thr->cgpu;
  33. uint8_t freqMDefault = data->freqMDefault;
  34. int i, maxM, bestM;
  35. double bestR, r;
  36. bool rv = true;
  37. if (freqMDefault > data->freqMaxM)
  38. // This occurs when the device in question adjusts its MaxM down due to temperature or similar reasons
  39. freqMDefault = data->freqMaxM;
  40. for (i = 0; i < data->freqMaxM; i++)
  41. if (data->maxErrorRate[i + 1] * i < data->maxErrorRate[i] * (i + 20))
  42. data->maxErrorRate[i + 1] = data->maxErrorRate[i] * (1.0 + 20.0 / i);
  43. maxM = 0;
  44. // Use max mulitplier up to the default as far as possible without hitting the max error rate
  45. while (maxM < freqMDefault && data->maxErrorRate[maxM + 1] < DCLK_MAXMAXERRORRATE)
  46. maxM++;
  47. // Use max mulitplier beyond the default if it's never hit the max error rate, and our current max has collected sufficient samples
  48. while (maxM < data->freqMaxM && data->maxErrorRate[maxM + 1] < DCLK_MAXMAXERRORRATE && data->errorWeight[maxM] >= data->minGoodSamples)
  49. maxM++;
  50. // Find the multiplier that gives the best hashrate
  51. bestM = data->freqMinM;
  52. bestR = 0;
  53. for (i = bestM; i <= maxM; i++) {
  54. // Hashrate is weighed on a linear scale
  55. r = (i + 1);
  56. // The currently selected frequency gets a small "bonus" in comparison, as hysteresis
  57. if (i == data->freqM)
  58. r += DCLK_ERRORHYSTERESIS;
  59. // Adjust for measured error rate
  60. r *= (1 - data->maxErrorRate[i]);
  61. // If it beats the current best, update best*
  62. if (r > bestR) {
  63. bestM = i;
  64. bestR = r;
  65. }
  66. }
  67. // Actually change the clock if the best multiplier is not currently selected
  68. if (bestM != data->freqM) {
  69. rv = changeclock(thr, bestM);
  70. }
  71. // Find the highest multiplier that we've taken a reasonable sampling of
  72. maxM = freqMDefault;
  73. while (maxM < data->freqMaxM && data->errorWeight[maxM + 1] > 100)
  74. maxM++;
  75. // If the new multiplier is some fraction of the highest we've used long enough to get a good sample, assume there is something wrong and instruct the driver to shut it off
  76. if ((bestM < (1.0 - DCLK_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  77. applog(LOG_ERR, "%"PRIpreprv": frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.",
  78. cgpu->proc_repr,
  79. (1.0 - 1.0 * bestM / maxM) * 100);
  80. return false;
  81. }
  82. return rv;
  83. }
  84. void dclk_gotNonces(struct dclk_data *data)
  85. {
  86. data->errorCount[data->freqM] *= 0.995;
  87. data->errorWeight[data->freqM] = data->errorWeight[data->freqM] * 0.995 + 1.0;
  88. }
  89. void dclk_errorCount(struct dclk_data *data, double portion)
  90. {
  91. data->errorCount[data->freqM] += portion;
  92. }
  93. void dclk_preUpdate(struct dclk_data *data)
  94. {
  95. data->errorRate[data->freqM] = data->errorCount[data->freqM] / data->errorWeight[data->freqM];
  96. // errorWeight 100 begins after sample period 137; before then, we minimize the effect of measured errorRate
  97. if (data->errorWeight[data->freqM] < 100)
  98. data->errorRate[data->freqM] /= 100;
  99. if (data->errorRate[data->freqM] > data->maxErrorRate[data->freqM])
  100. data->maxErrorRate[data->freqM] = data->errorRate[data->freqM];
  101. }